2013-10-17 52 views
0

我想構建一個android應用通過服務器發送圖像數據。首先,我想從我的應用程序發送文本數據到我的機器上的本地主機。我正在使用WAMP服務器。Android應用通過服務器發送圖像:wamp

我已經寫在日食此代碼,同時也對C語言創建的mypage.php:/ WAMP/WWW /文件夾

package com.example.httpexample; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://localhost/mypage.php"); 
     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4); 

      nameValuePairs.add(new BasicNameValuePair("fname", "vinod")); 
      nameValuePairs.add(new BasicNameValuePair("fphone", "1234567890")); 
      nameValuePairs.add(new BasicNameValuePair("femail", "[email protected]")); 
      nameValuePairs.add(new BasicNameValuePair("fcomment", "Help")); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      httpclient.execute(httppost); 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

但是當我運行模擬器,我不能看到數據是發送模擬器到localhost上的mytest.php文件。

我是這個領域的新手,可能沒有做一些正確的事情。請提出一條出路。如果您想要,請詢問更多詳情。另外我想告訴我的研究所使用代理服務器(但因爲我連接到我的機器應該不成問題)。

+0

請在此嘖嘖聲AsyncTask 6.5中的所有網絡資料http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html –

+0

也看看http://stackoverflow.com/questions/5806220/如何連接到我的http-localhost-web-server-from-android-emulator-in-eclips –

回答

1

使用多部分實體上傳服務器上的圖像數據或上傳圖像的本地link,並按照此link連接本地主機。

+0

是的,我現在能夠發送數據到我的本地主機。將按照鏈接完成圖像部分並回復給您。 –