I want upload image from android to spring controller. I already connected android with spring using rest api call (httpclient) My code is :
final String jsonUserMo = gson.toJson(userMO);
final StringBuilder contactLists = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
try {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody profileFile = new FileBody(profileImage);
builder.addPart("uploadImg", profileFile);
HttpEntity entity = builder.build();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userMO", jsonUserMo));
HttpPost post = new HttpPost(Constants.ROOTURL+"/media/uploadUserImage");
post.setEntity(entity);
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.i("Ringee",post.toString());
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
contactLists.append(rd.readLine());
} catch (Exception e) {
e.printStackTrace();
}
profile image is file. and its upload using mutipartentity But i got error in android
Caused by: java.lang.NoSuchMethodError: org.apache.http.entity.ContentType.create
My android dependencies :
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.1.11'
compile ('org.apache.httpcomponents:httpmime:4.4.1') {
exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
}
Error :
04-22 10:35:16.398 13604-14290/com.ringee.app E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
Process: com.ringee.app, PID: 13604
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.NoSuchMethodError: org.apache.http.entity.ContentType.create
at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:209)
at org.apache.http.entity.mime.MultipartEntityBuilder.build(MultipartEntityBuilder.java:230)
at com.ringee.app.delegates.MediaDelegates.uploadImageToServer(MediaDelegates.java:51)
at com.ringee.app.ImageUploadActivity$1$1.doInBackground(ImageUploadActivity.java:85)
at com.ringee.app.ImageUploadActivity$1$1.doInBackground(ImageUploadActivity.java:82)
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)
Looks like you use incompatible jar's for httpmime
and httpclient-android
. Try to use version 4.3.6
of httpmime
I was also doing same like you, then i have just used same library version and it worked,
I don't know how, but below code solved my problem
compile( 'org.apache.httpcomponents:httpmime:4.3.5') {
exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
NoSuchMethodError
Thrown if an application tries to call a specified method of a class (either static or instance), and that class no longer has a definition of that method. Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.
You should use incompatible version of both .
compile ('org.apache.httpcomponents:httpmime:4.5.2') {
exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient:4.5'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With