【问题标题】:MultipartEntity Files not uploading through deviceMultipartEntity 文件未通过设备上传
【发布时间】:2019-02-28 16:19:08
【问题描述】:

这可能是一个重复的问题,但我已经尝试了这个网站的所有答案,但没有一个有效!

我正在使用MultipartEntity 上传图片。它在模拟器中完全可以正常工作,但是当我签入设备时它无法正常工作。

在我的代码下面

 HttpParams params = new BasicHttpParams();
 params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP);    

 HttpClient httpClient = new DefaultHttpClient(params);
 HttpContext localContext = new BasicHttpContext();
 HttpPost httpPost = new HttpPost("http://url.com/webservice_uploadvideo.php");

 MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);

 FileBody filebodyVideopre = new FileBody(new File(videoUploadpathPre)); //pre

 entity.addPart("fin_detail", filebodyVideopre);

 httpPost.setEntity(entity);

 HttpResponse response = httpClient.execute(httpPost, localContext);

【问题讨论】:

  • 为什么要投反对票??
  • 这是我的博客,详细解释了如何在 android 中发出多部分请求 :) learnwithmehere.blogspot.in/2015_10_01_archive.html 仔细阅读 :) 这是我在 SO stackoverflow.com/questions/35839130/… 中的类似回答问题:)这是仅根据我的博客内容回答的:)看看他们两个:)顺便说一句,我不赞成你的问题:D
  • 从本教程中获得帮助,我在使用 Multipart 时也遇到了麻烦。 "androidhive.info/2014/12/…"
  • ` MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8"));`你应该这样修改
  • 你为什么还在使用已弃用的方法?

标签: java android httpclient


【解决方案1】:

这是我用于上传多部分图片的工作代码。是的,它适用于真实设备。

private int uploadImage(String selectedImagePath) {

  try {
    HttpClient client = new DefaultHttpClient();
    File file = new File(selectedImagePath);
    HttpPost post = new HttpPost(Constants.URL);

    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,
                    Constants.BOUNDARY, Charset.defaultCharset());

    entity.addPart(Constants.MULTIPART_FORM_DATA_NAME, new FileBody(file));
    post.setHeader("Accept", "application/json");
    post.setHeader("Content-Type", "multipart/form-data; boundary=" + Constants.BOUNDARY);

    post.setEntity(entity);

    HttpResponse response = client.execute(post);
    HttpEntity httpEntity = response.getEntity();

    int status = response.getStatusLine().getStatusCode();

    return status;

  } catch (Exception e) {
    e.printStackTrace();
    return null;
  }
}

【讨论】:

  • 只需在此处输入String。就我而言,它的“边界”。无论如何,任何字符串都可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-06
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多