2011-07-29 120 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

相关问题