2011-07-29 122 views
1

我想從android設備上傳一張照片到php網站。 爲此,我使用MultipartEntity編碼。將圖像從android上傳到php服務器

對此I added我的項目作爲external jar filehttpmime-4.1-beta1.jar

我想上傳到php的網站是image存儲在SDcard。 爲此我做在我的代碼如下:

HttpPost httppost = new HttpPost("....the link to the webiste..."); 

     MultipartEntity reqEntity = new MultipartEntity(); 
     StringBody sb=new StringBody("...."); 


     File file=new File("/sdcard/image.jpg"); 
     FileBody bin = new FileBody(file); 
     reqEntity.addPart("android",bin); 
     reqEntity.addPart("id", sb); 

     httppost.setEntity(reqEntity); 

     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity resEntity = response.getEntity(); 


     if (resEntity != null) { 
      String page = EntityUtils.toString(resEntity); 
      System.out.println("PAGE :" + page); 
     } 

但這樣做的問題是,從PHP服務器的響應總是斷開的鏈接。

我想嘗試下是使用

MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 

但不幸的是jar我進口不具有HttpMultipartMode.BROWSER_COMPATIBLE。所以該類我真的很感激,如果你能指出我朝着正確的方向,什麼否則我應該導入這個工作....或者我應該如何上傳圖像到服務器。 我必須說,服務器建立上傳照片是這樣的:

method="post" enctype="multipart/form-data" 
         name="form" target="_self" id="form"> 
        <input type="hidden" name="p" value="a" /> 

謝謝!

+0

你可能在尋找這樣的事情: http://stackoverflow.com/questions/5476625/android-simple-way-to -up-a-picture-to-a-image-host/5476726#5476726 – pawelzieba

回答

2

我建議不要使用beta庫。你應該使用apache-mime4j-0.6。您也需要httpmime-4.0.1

這些都需要你的進口:

 import org.apache.http.HttpResponse; 
     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.HttpGet; 
     import org.apache.http.client.methods.HttpPost; 
     import org.apache.http.entity.mime.HttpMultipartMode; 
     import org.apache.http.entity.mime.MultipartEntity; 
     import org.apache.http.entity.mime.content.FileBody; 
     import org.apache.http.entity.mime.content.StringBody; 
     import org.apache.http.impl.client.DefaultHttpClient; 
     import org.apache.http.message.BasicNameValuePair; 
+0

好的,incerc.Multumesc – adrian

+0

檢查我的編輯。 –

+0

現在它告訴我:無法解析類型org.apache.james.mime4j.message.SingleBody。它是從所需的.class 文件中間接引用的:\t MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart(「android」,bin); \t reqEntity.addPart(「id」,sb); – adrian

相關問題