In my android eclipse project, i want to upload image file with name and email fields to server.
But i got the following error : my logcat is following :
NoSuchFieldError - BasicHeaderValueFormatter.INSTANCE
E/AndroidRuntime(25348): Caused by: java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE
my entire logcat :
E/AndroidRuntime(25348): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(25348): Process: com.example.uploadfiles, PID: 25348
E/AndroidRuntime(25348): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(25348): at android.os.AsyncTask$3.done(AsyncTask.java:300)
E/AndroidRuntime(25348): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
E/AndroidRuntime(25348): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
E/AndroidRuntime(25348): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
E/AndroidRuntime(25348): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/AndroidRuntime(25348): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime(25348): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime(25348): at java.lang.Thread.run(Thread.java:841)
E/AndroidRuntime(25348): Caused by: java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE
E/AndroidRuntime(25348): at org.apache.http.entity.ContentType.toString(ContentType.java:153)
E/AndroidRuntime(25348): at org.apache.http.entity.mime.MultipartFormEntity.<init>(MultipartFormEntity.java:52)
E/AndroidRuntime(25348): at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:226)
E/AndroidRuntime(25348): at org.apache.http.entity.mime.MultipartEntityBuilder.build(MultipartEntityBuilder.java:230)
E/AndroidRuntime(25348): at com.example.uploadfiles.MainActivity$ImageUploadTask.doInBackground(MainActivity.java:172)
E/AndroidRuntime(25348): at com.example.uploadfiles.MainActivity$ImageUploadTask.doInBackground(MainActivity.java:1)
E/AndroidRuntime(25348): at android.os.AsyncTask$2.call(AsyncTask.java:288)
E/AndroidRuntime(25348): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime(25348): ... 4 more
My code for uploading image and some fields are following :
class ImageUploadTask extends AsyncTask<Void, Void, String> {
@SuppressWarnings("deprecation")
@Override
protected String doInBackground(Void... unsued) {
try {
File image = new File(iPath);
FileBody fileBody = new FileBody(image);
HttpClient client = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost post = new HttpPost(Constant.signUp);
post.setHeader("enctype", "multipart/form-data");
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("ID", new StringBody("1"));
multipartEntity.addPart("pID", new StringBody("1"));
multipartEntity.addPart("userPhoto", fileBody);
multipartEntity.addPart("email", new StringBody("joseph@gmail.com"));
multipartEntity.addPart("cell", new StringBody("1234567890"));
multipartEntity.addPart("username", new StringBody("joseph"));
post.setEntity(multipartEntity.build());
// post.setEntity((MultipartEntityBuilder) multipartEntity);
HttpResponse response = client.execute(post, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),
"UTF-8"));
HttpResponse responses = client.execute(post);
String responseBody = EntityUtils.toString(response.getEntity());
Log.v("multiPartPost HTTP Response", responseBody);
} catch (Exception e) {
System.out.println("error=" + e.getMessage());
return null;
}
return "";
}
@Override
protected void onProgressUpdate(Void... unsued) {
}
@Override
protected void onPostExecute(String sResponse) {
try {
if (sResponse != null) {
JSONObject JResponse = new JSONObject(sResponse);
int success = JResponse.getInt("SUCCESS");
String message = JResponse.getString("MESSAGE");
if (success == 0) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Photo uploaded successfully", Toast.LENGTH_SHORT)
.show();
caption.setText("");
}
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "excepttion", Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
}
My libs folder have following necessary libraries :
And in my java build path :
and in order-export in properties
Now i what have to do to solve this error : i working entire day for solving this but not, so please any one suggest me to how to solve this error, it very appreciated, thank you.
This is main problem of libs., that all might faced, wrong library causes this issue,
So, following step helpful to you :
(1) Code : Following code is successfully upload photo to the server :
StoreByteImage(...) :
Class ImageUploadTask
(2) First Remove All libs/jar file from the your libs Folder :
(3) Find Following libraries/jar file from the internet and put in libs folder :
(4) Add jar from the libraries tab :
(5) Your Order and Export libraries/jar file Tick mark same as following :
Ok, Done. Just these simple steps helpful to you upload image file to server. hope these helphul to you and others.
I have upgraded from 4.3 to 4.4 before i have noticed this error. Could be 4.3 causing it.