在我的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$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
Eclipse启动时崩溃(eclipse.exe已停止工作),任何解决scheme?
无法将某些红外遥控钥匙代码传递给Android
Android LocalServerSocket
Gradle离线如何caching依赖关系
从Android源(AOSP)构build一个工具
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构buildpath:
并按顺序出口物业
现在我该怎么办才能解决这个错误:我一整天都在为解决这个问题而努力,所以请任何一位build议我如何解决这个错误,非常感谢,谢谢。
在Android上查找SD卡卷标
在Fedora 64位Android模拟器使用哪个libGL?
如何在Android平台上检查init.rc(init进程)的输出?
如何在Android Studio 1.0中findfilterLogcatconfiguration?
这是libs的主要问题,这一切都可能面临,错误的图书馆导致这个问题,
所以,下面的步骤对您有帮助:
btnUpload.setonClicklistner(new View.OnClicklistner() { @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; }
类ImageUploadTask
class ImageUploadTask extends AsyncTask<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,"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(),e); } } }
(2)首先从你的libs文件夹中删除所有libs / jar文件:
(3)从互联网上查找以下库/ jar文件并放在libs文件夹中:
(4)从库选项卡添加jar: