android multipartentity 怎么上传参数,Android的上传文件到服务器使用FileEntity并没有使用MultipartEntity...

该博客探讨了在Android应用中使用`FileEntity`尝试上传文件到服务器时遇到的问题。博主指出,服务器端反馈显示文件未成功上传,且`$_FILES`变量为空。由于`MultipartEntity`在Android原生库中不可用,博主寻求使用`FileEntity`的解决方案,并希望找到不依赖第三方库的教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public void PostFile() {

HttpClient httpclient = new DefaultHttpClient();

httpclient.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

// httpclient.

HttpPost httppost = new HttpPost("http://fastcalc.orionsource.ru/test.php");

File file = cache.getFileFromCache("citys.json");

FileEntity reqEntity = new FileEntity(file,"text/plain");

reqEntity.setContentType("text/plain");

httppost.setEntity(reqEntity);

System.out.println("executing request " + httppost.getRequestLine());

HttpResponse response;

try {

response = httpclient.execute(httppost);

HttpEntity resEntity = response.getEntity();

System.out.println(response.getStatusLine());

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();

}

httpclient.getConnectionManager().shutdown();

}

在服务器端代码如下

print 'Files:';

print_r($_FILES);

print '
';

print "Response:\n";

print_r($_REQUEST);

print '
';

?>

而在Android上,我得到回应

05-29 17:26:29.232: I/System.out(26823): executing request POST http://fastcalc.orionsource.ru/test.php HTTP/1.1

05-29 17:26:29.732: I/System.out(26823): HTTP/1.1 200 OK

05-29 17:26:29.732: I/System.out(26823): Files:Array

05-29 17:26:29.732: I/System.out(26823): (

05-29 17:26:29.732: I/System.out(26823):)

05-29 17:26:29.732: I/System.out(26823):
Response:

05-29 17:26:29.732: I/System.out(26823): Array

05-29 17:26:29.732: I/System.out(26823): (

05-29 17:26:29.732: I/System.out(26823):)

05-29 17:26:29.732: I/System.out(26823):

我不能使用MultipartEntity,因为它不包含在任何Android版本中。它是第三部分图书馆。

有人给请链接到只使用FileEntity的教程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值