android multipartentity 多张图片,android – 帮助用MultipartEntity构建一个POST请...

我想使用以下参数构造一个多部分请求:name(字符串),email(字符串)和fileupload(文件).我正在使用下面的Java代码(在Android中工作).

打印httppost.getRequestLine()

POST http://www.myurl.com/upload HTTP/1.1

因此,客户端站点上的一切看起来都很好,但我的服务器(Django / Apache)将其作为GET请求读取,没有GET参数 – request.method生成’GET’,request.GET.items()生成一个空字典.

我究竟做错了什么?我实际上并不知道如何正确设置多部分参数 – 使用猜测 – 所以这可能是问题所在.

public void SendMultipartFile() {

Log.e(LOG_TAG, "SendMultipartFile");

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("http://www.myurl.com/upload");

File file = new File(Environment.getExternalStorageDirectory(),

"video.3gp");

Log.e(LOG_TAG, "setting up multipart entity");

MultipartEntity mpEntity = new MultipartEntity();

ContentBody cbFile = new FileBody(file);

mpEntity.addPart("fileupload", cbFile);

Log.i("SendLargeFile", "file length = " + file.length());

try {

mpEntity.addPart("name", new StringBody(name));

mpEntity.addPart("email", new StringBody(email));;

} catch (UnsupportedEncodingException e1) {

// TODO Auto-generated catch block

Log.e(LOG_TAG, "UnsupportedEncodingException");

e1.printStackTrace();

}

httppost.setEntity(mpEntity);

Log.e(LOG_TAG, "executing request " + httppost.getRequestLine());

HttpResponse response;

try {

Log.e(LOG_TAG, "about to execute");

response = httpclient.execute(httppost);

Log.e(LOG_TAG, "executed");

HttpEntity resEntity = response.getEntity();

Log.e(LOG_TAG, response.getStatusLine().toString());

if (resEntity != null) {

System.out.println(EntityUtils.toString(resEntity));

}

if (resEntity != null) {

resEntity.consumeContent();

}

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值