嘿伙计我是Android应用程序开发的新手,我正在尝试使用AsyncTask将文件上传到服务器,但是当我点击上传按钮应用程序崩溃时,请帮助解决这个问题。(抱歉,如果有任何语言错误)。
UploadFileToServer
上课
/**
* Uploading the file to server
* */
private class UploadFileToServer extends AsyncTask<Void, Integer, String> {
@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
return uploadFile();
}
@SuppressWarnings("deprecation")
private String uploadFile() {
String responseString;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL);
try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
new AndroidMultiPartEntity.ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
File sourceFile = new File(filePath);
// Adding file data to http body
entity.addPart("image",new FileBody(sourceFile));//new FileBody(sourceFile)
// Extra parameters pass to server
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;
}
logcat错误:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
#1. E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
#2.Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
E/AndroidRuntime: at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:89)
完整错误窗口如下:
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at java.lang.Thread.run(Thread.java:856)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:89)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at com.appnings.cameratrial.UploadActivity$UploadFileToServer.uploadFile(UploadActivity.java:166)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at com.appnings.cameratrial.UploadActivity$UploadFileToServer.doInBackground(UploadActivity.java:139)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at com.appnings.cameratrial.UploadActivity$UploadFileToServer.doInBackground(UploadActivity.java:114)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: at java.lang.Thread.run(Thread.java:856)
10-18 20:51:23.440 569-649/? E/android.os.Debug: !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
答案 0 :(得分:0)
鉴于你没有发布错误日志(来自logcat),很难猜到。
你可能错过的常见事情:
<uses-permission android:name="android.permission.INTERNET" />
在Manifest.xml中的应用程序标记之外
答案 1 :(得分:0)
尝试导入不同的Apache lib文件,某些lib文件中的某些类最终在它运行了几次导入之后。谢谢你们。快乐的编码。
答案 2 :(得分:0)
1.使用正确的方法添加罐子。
右键单击您的lib文件夹并选择“添加库”,然后输入到项目。
packagingOptions {
排除'META-INF / DEPENDENCIES' 排除'META-INF / NOTICE' 排除'META-INF / LICENSE' 排除'META-INF / LICENSE.txt' 排除'META-INF / NOTICE.txt' }