未通过设备上传的多部分实体文件



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

我正在使用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);

这是我用于多部分图像上传的工作代码。是的,它可以在真实设备中工作。

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;
  }
}

最新更新