Android HttpClient Post文件多亲子关系



我正在尝试从android应用程序上传到使用HttpClient和multiparentity php web服务器的图像。

下面是我的代码片段:

protected Void doInBackground(Void... params) {
    HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpPost post = new HttpPost( "http://URL/upload.php" );
    try {
        MultipartEntity entity = new MultipartEntity(  );
        entity.addPart("type", new StringBody("photo"));
        entity.addPart("data", new FileBody(new File (this.path)));         
        post.setEntity(entity);
        post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);             
        post.addHeader( "Content-Type", "multipart/form-data; ");
        HttpResponse response = client.execute(post);
        HttpEntity resEntity = response.getEntity();
        System.out.println(response.getStatusLine());
        client.getConnectionManager().shutdown();
    } catch (UnsupportedEncodingException e) {          
        e.printStackTrace();
    } catch (ClientProtocolException e) {           
        e.printStackTrace();
    } catch (IOException e) {           
        e.printStackTrace();
    }
    return null;
}

在php文件中,我添加了print_r($_FILE)来查看数组的内容,它显示了一个空数组。

LogCat在执行HttpClient

后显示如下
06-17 14:54:03.908: I/System.out(1256): HTTP/1.1 200 OK
06-17 14:54:03.998: D/111(1256): Array
06-17 14:54:03.998: D/111(1256): (
06-17 14:54:03.998: D/111(1256): )
06-17 14:54:04.068: D/dalvikvm(1256): GC_CONCURRENT freed 200K, 12% free 2559K/2900K, paused 96ms+5ms, total 161ms

谁能告诉我我需要在Android代码中添加什么?

根据关于使用multiparentity将数据发布到url的博客文章,您可能需要在项目中包含一些额外的jar文件。它们包括以下apache开源项目:apache-mime4j, httpclient, httpcore和httpmime。

在构造函数中添加:

MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

相关内容

  • 没有找到相关文章

最新更新