API 22上的MultipartEntityBuilder

时间:2015-10-08 13:11:20

标签: android file-upload image-uploading multipartentity android-5.1.1-lollipop

我想将图像上传到服务器,但我需要再发送2个东西,以便与服务器连接任何帮助,因为我真的卡在这个上。

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
           // MultipartEntity entity = new MultipartEntity();
            try {



               File file = new File(path);
                // Adding file data to http body
                builder.addPart("image", new FileBody(file));

                // Extra parameters if you want to pass to server
                builder.addTextBody("confid", name);
                builder.addTextBody("udid", id);

                entity = builder.build();
                httppost.setEntity(entity);

                // Making server call
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity r_entity = response.getEntity();

                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    // Server response
                    responseString = EntityUtils.toString(r_entity);
                } else {
                    responseString = "Error occurred! Http Status Code: "
                            + statusCode;
                }

            } catch (ClientProtocolException e) {
                responseString = e.toString();
            } catch (IOException e) {
                responseString = e.toString();
            }

            return responseString;

        }

以下是android Studio logcat的错误

     0-08 12:06:01.216    8616-8787/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)
 Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.mime.MultipartEntityBuilder
        at com.example.oleg.myapplication.MainActivity$1.uploadFile(MainActivity.java:305)
        at com.example.oleg.myapplication.MainActivity$1.doInBackground(MainActivity.java:285)         at com.example.oleg.myapplication.MainActivity$1.doInBackground(MainActivity.java:279)  at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at  java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at        java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)

0 个答案:

没有答案