使用MultipartEntityBuilder上载文件时出现错误



在我的android应用程序中,我使用AsyncTask将文件上传到后端。在那里,我在doInBackground方法中使用了MultipartEntityBuilder。我的doInBackground部分如下,

@Override
protected String doInBackground(String... params) {
    try {
        String serverLoaction = params[0];
        String filePath = params[1];
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(serverLoaction);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        FileBody fileBody = new FileBody(new File(filePath));
        StringBody stringBody = new StringBody("data", ContentType.MULTIPART_FORM_DATA);
        builder.addPart("file", fileBody);
        builder.addPart("name", stringBody);
        httpPost.setEntity(builder.build());
        HttpResponse response = httpClient.execute(httpPost, localContext);
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                        response.getEntity().getContent(), "UTF-8"));
        String sResponse = reader.readLine();
        return sResponse;
    } catch (Exception e) {
        if (dialog.isShowing())
            dialog.dismiss();
        Toast.makeText(getApplicationContext(), "Error in downloading image", Toast.LENGTH_LONG).show();
        Log.e(e.getClass().getName(), e.getMessage(), e);
        return null;
    }
}

这显示我没有语法错误。但当我运行代码时,一个异常抛出,应用程序停止。错误日志如下。

Process: com.x.x, PID: 6271
 java.lang.RuntimeException: An error occured while executing doInBackground()
 at android.os.AsyncTask$3.done(AsyncTask.java:300)
 at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
 at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
 at java.util.concurrent.FutureTask.run(FutureTask.java:242)
 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
 at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE
 at org.apache.http.entity.ContentType.toString(ContentType.java:153)
 at org.apache.http.entity.mime.MultipartFormEntity.<init>(MultipartFormEntity.java:56)
 at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:236)
 at org.apache.http.entity.mime.MultipartEntityBuilder.build(MultipartEntityBuilder.java:240)
 at com.x.x.ui.farm_activity.AddCropMonitors$UploadFileTask.doInBackground(AddCropMonitors.java:1905)
 at com.x.x.ui.farm_activity.AddCropMonitors$UploadFileTask.doInBackground(AddCropMonitors.java:1875)
 at android.os.AsyncTask$2.call(AsyncTask.java:288)
 at java.util.concurrent.FutureTask.run(FutureTask.java:237)
 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
 at java.lang.Thread.run(Thread.java:841)

它告诉NoSuchFieldError异常引发,但我看了几个教程,根据它们,代码似乎是可以的。发生错误的行号指向

httpPost.setEntity(builder.build());

行。那么这个代码段有什么问题,我该如何解决。谢谢和问候!

我认为这是由于您在build.gradle文件中使用的库。请使用以下代码检查一次

compile('org.apache.httpcomponents:httpmime:4.3.6') {
exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5'

将其添加到build.gradle文件中。

implementation 'org.apache.httpcomponents:httpmime:4.5.10'

相关内容

  • 没有找到相关文章

最新更新