Android 文件上传到服务器时出现 eclipse 错误
Android eclipse error on File upload to server
在我的 android eclipse 项目中,我想将带有名称和电子邮件字段的图像文件上传到服务器。
但我收到以下错误:我的 logcat 如下:
NoSuchFieldError - BasicHeaderValueFormatter.INSTANCE
E/AndroidRuntime(25348): Caused by: java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE
我的全部 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.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.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.call(AsyncTask.java:288)
E/AndroidRuntime(25348): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime(25348): ... 4 more
我上传图片的代码和一些字段如下:
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);
}
}
}
我的 libs 文件夹有以下必要的库:
在我的 java 构建路径中:
并按顺序导出属性
现在我必须做些什么来解决这个错误:我整天都在努力解决这个问题但没有,所以请任何人建议我如何解决这个错误,非常感谢,谢谢。
在我注意到这个错误之前,我已经从 4.3 升级到 4.4。
可能是 4.3 造成的。
这是库的主要问题,所有人都可能遇到,错误的库导致了这个问题,
所以,以下步骤对您有帮助:
(1)代码:以下代码成功上传照片到服务器:
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (Constant.bitmapPicture != null) {
StoreByteImage(Constant.bitmapPicture, 90, "my_image");
} else {
Toast.makeText(getApplicationContext(), "image null", Toast.LENGTH_LONG).show();
}
}
});
StoreByteImage(...) :
public boolean StoreByteImage(Bitmap bitmap, int quality, String expName) {
FileOutputStream fileOutputStream = null;
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File myNewFolder = new File(extStorageDirectory + "/Example");
if (myNewFolder.mkdirs()) {
myNewFolder.mkdir();
}
try {
iPath = myNewFolder + "/" + expName + ".jpg";
fileOutputStream = new FileOutputStream(myNewFolder + "/" + expName + ".jpg");
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bitmap.compress(CompressFormat.JPEG, quality, bos);
new ImageUploadTask().execute();
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
Class 图片上传任务
class ImageUploadTask extends AsyncTask<Void, Void, String> {
String sResponse;
@SuppressWarnings("deprecation")
@Override
protected String doInBackground(Void... unsued) {
try {
//
File image = new File(iPath);
FileBody fileBody = new FileBody(image);
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(YourImageUploadURLHere);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Constant.bitmapPicture.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
entity.addPart("ID", new StringBody("1"));
entity.addPart("pID", new StringBody("1"));
entity.addPart("userPhoto", fileBody);
entity.addPart("email", new StringBody("email@gmail.com"));
entity.addPart("cell", new StringBody("1234567890"));
entity.addPart("username", new StringBody("name"));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),
"UTF-8"));
sResponse = reader.readLine();
System.out.println("responce=" + sResponse);
} catch (Exception e) {
System.out.println("error=" + e.getMessage());
return null;
}
return "";
}
@Override
protected void onProgressUpdate(Void... unsued) {
}
@Override
protected void onPostExecute(String sResponses) {
try {
if (sResponses != null) {
JSONObject jsonObjSend = new JSONObject(sResponse.toString());
if (jsonObjSend.getString("status").equals("success")) {
// another code
} else if (jsonObjSend.getString("status").equals("fail")) {
// another code
}
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "excepttion", Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
}
(2) 首先从您的 libs 文件夹中删除所有 libs/jar 文件:
(3) 从网上找到以下libraries/jar文件,放入libs文件夹:
(4) 从库选项卡添加 jar :
(5) Your Order and Export libraries/jar file 勾号如下:
好的,完成。只需这些简单的步骤即可帮助您将图像文件上传到服务器。
希望这些对你和其他人有帮助。
在我的 android eclipse 项目中,我想将带有名称和电子邮件字段的图像文件上传到服务器。
但我收到以下错误:我的 logcat 如下:
NoSuchFieldError - BasicHeaderValueFormatter.INSTANCE
E/AndroidRuntime(25348): Caused by: java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE
我的全部 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.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.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.call(AsyncTask.java:288)
E/AndroidRuntime(25348): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime(25348): ... 4 more
我上传图片的代码和一些字段如下:
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);
}
}
}
我的 libs 文件夹有以下必要的库:
在我的 java 构建路径中:
现在我必须做些什么来解决这个错误:我整天都在努力解决这个问题但没有,所以请任何人建议我如何解决这个错误,非常感谢,谢谢。
在我注意到这个错误之前,我已经从 4.3 升级到 4.4。 可能是 4.3 造成的。
这是库的主要问题,所有人都可能遇到,错误的库导致了这个问题,
所以,以下步骤对您有帮助:
(1)代码:以下代码成功上传照片到服务器:
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (Constant.bitmapPicture != null) {
StoreByteImage(Constant.bitmapPicture, 90, "my_image");
} else {
Toast.makeText(getApplicationContext(), "image null", Toast.LENGTH_LONG).show();
}
}
});
StoreByteImage(...) :
public boolean StoreByteImage(Bitmap bitmap, int quality, String expName) {
FileOutputStream fileOutputStream = null;
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File myNewFolder = new File(extStorageDirectory + "/Example");
if (myNewFolder.mkdirs()) {
myNewFolder.mkdir();
}
try {
iPath = myNewFolder + "/" + expName + ".jpg";
fileOutputStream = new FileOutputStream(myNewFolder + "/" + expName + ".jpg");
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bitmap.compress(CompressFormat.JPEG, quality, bos);
new ImageUploadTask().execute();
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
Class 图片上传任务
class ImageUploadTask extends AsyncTask<Void, Void, String> {
String sResponse;
@SuppressWarnings("deprecation")
@Override
protected String doInBackground(Void... unsued) {
try {
//
File image = new File(iPath);
FileBody fileBody = new FileBody(image);
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(YourImageUploadURLHere);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Constant.bitmapPicture.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
entity.addPart("ID", new StringBody("1"));
entity.addPart("pID", new StringBody("1"));
entity.addPart("userPhoto", fileBody);
entity.addPart("email", new StringBody("email@gmail.com"));
entity.addPart("cell", new StringBody("1234567890"));
entity.addPart("username", new StringBody("name"));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),
"UTF-8"));
sResponse = reader.readLine();
System.out.println("responce=" + sResponse);
} catch (Exception e) {
System.out.println("error=" + e.getMessage());
return null;
}
return "";
}
@Override
protected void onProgressUpdate(Void... unsued) {
}
@Override
protected void onPostExecute(String sResponses) {
try {
if (sResponses != null) {
JSONObject jsonObjSend = new JSONObject(sResponse.toString());
if (jsonObjSend.getString("status").equals("success")) {
// another code
} else if (jsonObjSend.getString("status").equals("fail")) {
// another code
}
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "excepttion", Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
}
(2) 首先从您的 libs 文件夹中删除所有 libs/jar 文件:
(3) 从网上找到以下libraries/jar文件,放入libs文件夹:
(4) 从库选项卡添加 jar :
(5) Your Order and Export libraries/jar file 勾号如下:
好的,完成。只需这些简单的步骤即可帮助您将图像文件上传到服务器。 希望这些对你和其他人有帮助。