Android的HttpClient的帖子文件MultipartEntity [英] Android HttpClient Post File MultipartEntity

查看:197
本文介绍了Android的HttpClient的帖子文件MultipartEntity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个应用程序的Adroid上传图片使用PHP的Web服务器的HttpClient和MultipartEntity。

I am trying to upload an image from an Adroid app to a php webserver using HttpClient and MultipartEntity.

下面是我的code的一个片段:

Here is a snippet of my code :

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)看到阵列的内容,它显示一个空数组。

in the php file I've added print_r($_FILE) to see the content of the Array, and it's showing an empty array.

LogCat中是显示在执行后,以下HttpClient的

LogCat is showing the following after executing the 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 code?

Can anybody tell me what I have to add to the Android code ?

推荐答案

在构造函数中添加这样的:

Add this in your constructor:

MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

这篇关于Android的HttpClient的帖子文件MultipartEntity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆