简体   繁体   中英

Image Uploading using multipart entity Error

im using multipart entity to upload to my image to server.this is my code

private void uploadFile() {
             try {
            HttpClient httpclient = new DefaultHttpClient();
             httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
             HttpPost httppost = new HttpPost(rest_url);

             Map<String, Object> data = new LinkedHashMap<String, Object>();
                data.put(SESSION, sessionId);
                data.put(MODULE_NAME, "POD_POD");

                String restData = org.json.simple.JSONValue.toJSONString(data);


             File file = new File(signature_path);
             MultipartEntity mpEntity = new MultipartEntity();
             ContentBody cbFile = new FileBody(file, "image/jpeg");
            // mpEntity.addPart("userfile", cbFile); 

             mpEntity.addPart(METHOD, new StringBody(SET_ENTRY));
             mpEntity.addPart(INPUT_TYPE, new StringBody(JSON));
             mpEntity.addPart(RESPONSE_TYPE, new StringBody(JSON));
             mpEntity.addPart(REST_DATA, new StringBody(restData));

                mpEntity.addPart("name",
                         new StringBody("uploadfile"));

             // Adding file data to http body
             mpEntity.addPart("value", cbFile);

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

             HttpResponse response;


                response = httpclient.execute(httppost);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

This is Logcat

FATAL EXCEPTION: main
Process: com.stonewheeltest, PID: 18823
java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:85)
at newCrm.Sugercrm_sync.uploadFile(Sugercrm_sync.java:207)
at newCrm.Sugercrm_sync.access$0(Sugercrm_sync.java:192)
at newCrm.Sugercrm_sync$AddNewRecord.onPostExecute(Sugercrm_sync.java:185)
at newCrm.Sugercrm_sync$AddNewRecord.onPostExecute(Sugercrm_sync.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)

Anyone can help me..

I had a similar problem, for me adding the following dependencies solved the problem:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:20.+'
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
    compile('org.apache.httpcomponents:httpmime:4.3') {
        exclude module: "httpclient"
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM