本文介绍了使用多实体错误上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用多实体上传到我的形象server.this IM是我的code

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

这是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"
    }

}

这篇关于使用多实体错误上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 08:09