我试图在Android应用程序中使用Async任务上传文件,但是收到以下错误。
FATAL EXCEPTION: AsyncTask #5
Process: zupportdesk.desk.zupport.chatsystem, PID: 15325
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:318)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/entity/ContentType;
at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:89)
at zupportdesk.desk.zupport.chatsystem.Chatting$UploadFileToServer.uploadFile(Chatting.java:507)
at zupportdesk.desk.zupport.chatsystem.Chatting$UploadFileToServer.doInBackground(Chatting.java:483)
at zupportdesk.desk.zupport.chatsystem.Chatting$UploadFileToServer.doInBackground(Chatting.java:453)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
10-11 13:58:54.157 15325-16388/zupportdesk.desk.zupport.chatsystem E/AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.entity.ContentType" on path: DexPathList[[dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-support-annotations-24.2.0_f3465f35ba4af4c2d28c6ee70c3e045c2eeb50a7-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-signalr-client-sdk_31f218879a3e501695cc773014efa2b96309ba8b-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-signalr-client-sdk-android_9ac6c75fc433d5053cb8f4baaf075be77ff080a9-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-net.gotev-uploadservice-2.1_2c1cea94bdc9eb3e8e9259ef14480ae4e93a8a89-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-internal_impl-24.2.0_f88755d908402b42bce774f931195b94097dcd23-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-internal_impl-24.2.0_b1513c66b0f7395a0d1a480c5d9e7610feba9993-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-internal_impl-24.2.0_a54c1e5a7bced7e81fab3aff91c3679ae60c76ef-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-internal_impl-24.2.0_460fbcae657ae69586227c7cdd69197470f7a011-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-internal_impl-24.2.0_318b533f27c268e47e239190dc0e3679ce16407b-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-internal_impl-22.0.0_abd26134702fe065270338b254592d1345086842-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-httpmime-4.3.1_138604929e9825b5f1d61e00fa3d052bdd42ee07-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-gson-2.2.2_dd1fd3287d2711d7941193e32d486f688b74a811-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-wearable-7.5.0_8bc4b86a60416c7425dbe185fba38279931f0335-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-wallet-7.5.0_4efd5a84b19b11d4c433540cf235021f2dde600a-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-safetynet-7.5.0_da536d78f5e4cb7af22bd8628a84eea7c432183f-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-plus-7.5.0_955016b9062dcfdf301396658c4fb8b8d50d2cda-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-panorama-7
10-11 13:58:54.183 15325-16388/zupportdesk.desk.zupport.chatsystem E/AndroidRuntime: .5.0_5ad6e21e07aea6aa605b0a5a3d1e8c7d0d9916e5-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-nearby-7.5.0_4d90a5fe7ec8604f44a91517b9706bb315df6f6f-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-maps-7.5.0_581db932c15b767090a5e5f802baaddefdfbcb69-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-location-7.5.0_63bae70f0b75fd08770c6904e2c5023145952809-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-identity-7.5.0_65fed345583e13065e8aecba0d614d6e8b08c804-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-gcm-7.5.0_9937f9cc346a5a9ce714e33d1c59a2888a8f0175-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-games-7.5.0_e28cc480cf7ff3ef1f7670efd2deb91360d6de7d-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-fitness-7.5.0_745c20df06971dabc41d92c75dc6f7e0deb9e89c-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-drive-7.5.0_e0d26e2ebfc31a98ef927fa8e6406f47dbddba72-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-cast-7.5.0_9e2bcce3e42366ea376a964a0275e31a18d26639-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-base-7.5.0_837427f2c67624d2b611e73aedf619eb9e7adcda-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-appstate-7.5.0_d24931de1d4f9d1b51add7807a0e7928d761d6ea-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-appinvite-7.5.0_8ea4359777577a9d26e2c76942d7eeb7115cbc5b-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-appindexing-7.5.0_e13e83bb97402b3808744bad8ba85dd20bfca24c-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-analytics-7.5.0_ff810a4f13c82ad0651bd19ea6b1f3f7509079b3-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.google.android.gms-play-services-ads-7.5.0_721a4a78ddacc95366f18f8565d3b30cd6fdf18b-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.balysv-material-ripple-1.0.2_deb9b61de945d67e79032c223e41546a0ad447c7-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.android.support-support-vector-drawable-24.2.0_a54a9f6fe7e8cb1731b3190a43cac8d85c588bf6-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.android.support-support-v4-24.2.0_c76293055897ca4fcab423bcb13d1cff84d22510-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.android.support-support-media-compat-24.2.0_9b309206adafc25f2f7eb40e80a02f096ba5eacd-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.android.support-support-fragment-24.2.0_31cea7222b602999c550da8c3517862a0e6dab6c-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.android.support-support-core-utils-24.2.0_e5f66c7d6ab2d7093b22eaac2d9901a3a0ed6c94-classes.dex", dex fi
10-11 13:58:54.214 15325-16388/zupportdesk.desk.zupport.chatsystem E/AndroidRuntime: le "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.android.support-support-core-ui-24.2.0_f971c230898c04a60891a5b65c3bcf595b771e02-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.android.support-support-compat-24.2.0_a7085c5673ade8185ff028973f38d4ef027644a7-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.android.support-recyclerview-v7-24.2.0_eeeee17220ba35dd6071f6329854feb74a32672d-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.android.support-mediarouter-v7-22.0.0_5b2554495e1a4a2428c8836ad086b43d046da6a8-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.android.support-design-24.2.0_024e7eb976d8157d6e7efa9de71089c42159bcb6-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.android.support-cardview-v7-24.2.0_9575e625779a52a14eae5bd3d3931d81b65e8586-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.android.support-appcompat-v7-24.2.0_422b025bd9c2dbb23e739516684717e9474b91e5-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-24.2.0_396d53bb3e16626f132b0f0628fec5980fb13370-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-br.com.liveo-navigationdrawer-material-2.5.1_50ba57259819ff85adaa100f90f5389530cf9d32-classes.dex", dex file "/data/data/zupportdesk.desk.zupport.chatsystem/files/instant-run/dex/slice-Java-WebSocket-1.3.0_485a7232d8572525d79567164b42d2d35fc50231-classes.dex"],nativeLibraryDirectories=[/data/app/zupportdesk.desk.zupport.chatsystem-1/lib/arm64, /system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader.findClass(IncrementalClassLoader.java:90)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 10 more
方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
// imageView.setImageBitmap(bitmap);
//uploadMultipart();
String path = getPath(filePath);
File file = new File(path);
long length = file.length();
String nameFile = file.getName();
String extension = getFileExt(nameFile);
length = length/1024;
new UploadFileToServer(profileToken, nameFile).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class UploadFileToServer extends AsyncTask<Void, Integer, String> {
String token;
String fileName;
public UploadFileToServer(String profileToken, String FileName) {
this.token = profileToken;
this.fileName = FileName;
}
@Override
protected void onPreExecute() {
// setting progress bar to zero
// progressBar.setProgress(0);
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... progress) {
// Making progress bar visible
// progressBar.setVisibility(View.VISIBLE);
// updating progress bar value
// progressBar.setProgress(progress[0]);
// updating percentage value
//txtPercentage.setText(String.valueOf(progress[0]) + "%");
Toast.makeText(getApplicationContext(), String.valueOf(progress[0])+"%" , Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(Void... params) {
return uploadFile();
}
@SuppressWarnings("deprecation")
private String uploadFile() {
String responseString = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(UPLOAD_URL);
httppost.addHeader("Authorization", "Bearer "+token);
try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
new AndroidMultiPartEntity.ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
File sourceFile = new File(String.valueOf(filePath));
// Adding file data to http body
//entity.addPart("file", new FileBody(sourceFile));
entity.addPart("file", new FileBody(sourceFile, "image/jpeg"));
// Extra parameters if you want to pass to server
entity.addPart("name", new StringBody(fileName));
// entity.addPart("email", new StringBody("abc@gmail.com"));
totalSize = entity.getContentLength();
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: "
+ statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (IOException e) {
responseString = e.toString();
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
Log.d("Upload Results", result);
// showing the server response in an alert dialog
showAlert(result);
super.onPostExecute(result);
}
}
/**
* Method to show alert dialog
* */
private void showAlert(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message).setTitle("Response from Servers")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing
}
});
AlertDialog alert = builder.create();
alert.show();
}
摇篮
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "MY APPLICATION ID.chatsystem"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile files('libs/gson-2.2.2.jar')
compile files('libs/signalr-client-sdk-android.jar')
compile files('libs/signalr-client-sdk.jar')
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:recyclerview-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
compile 'com.android.support:cardview-v7:24.2.0'
compile 'br.com.liveo:navigationdrawer-material:2.5.1'
compile 'org.java-websocket:Java-WebSocket:1.3.0'
compile 'com.google.android.gms:play-services:7.5.0'
compile files('libs/httpmime-4.3.1.jar')
compile 'net.gotev:uploadservice:2.1'
compile ('org.apache.httpcomponents:httpcore:4.4.1'){
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
}
答案 0 :(得分:0)
您在运行时缺少类ContentType: 引起:java.lang.NoClassDefFoundError:解析失败:Lorg / apache / http / entity / ContentType;
org.apache.httpcomponents:的HttpCore:4.x.x
也许这有助于: Problems including Apache HttpComponents in Android Gradle project