我使用multipart实体上传到我的图像到server.this是我的代码
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)
任何人都可以帮助我..
答案 0 :(得分:3)
我有类似的问题,对我来说,添加以下依赖项解决了问题:
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"
}
}