使用 AsyncTask 将图片或视频上传到服务器
upload Image or video to server using AsyncTask
大家好,我是 android 应用程序开发的新手,我正在尝试使用 AsyncTask 将文件上传到服务器,但是当我单击上传按钮时应用程序崩溃了,请帮忙解决这个问题。(抱歉如果有任何语言错误)。
UploadFileToServer
class
/**
* 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)
完整错误window如下:
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.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.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.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.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
鉴于您尚未发布错误日志(来自 logcat),很难猜测。
您可能错过的常见事物:
<uses-permission android:name="android.permission.INTERNET" />
在 Manifest.xml 中的应用程序标记之外
尝试导入不同的 Apache lib 文件,一些 类 在一些 lib 文件中丢失,最终在几次导入后它起作用了。谢谢你们。快乐的编码。
我解决了同样的问题
1.Use添加jar的正确方法
Right click your lib folder and choice "add library", and input to project.
- 在settings.gradle中添加。
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
- 编译环境:
- Android Studio 1.4
- 目标sdk:21
- jars:httpclient-4.3.6.jar,httpcore-4.3.3.jar,httpmime-4.3.6.jar
大家好,我是 android 应用程序开发的新手,我正在尝试使用 AsyncTask 将文件上传到服务器,但是当我单击上传按钮时应用程序崩溃了,请帮忙解决这个问题。(抱歉如果有任何语言错误)。
UploadFileToServer
class
/**
* 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)
完整错误window如下:
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.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.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.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.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
鉴于您尚未发布错误日志(来自 logcat),很难猜测。
您可能错过的常见事物:
<uses-permission android:name="android.permission.INTERNET" />
在 Manifest.xml 中的应用程序标记之外
尝试导入不同的 Apache lib 文件,一些 类 在一些 lib 文件中丢失,最终在几次导入后它起作用了。谢谢你们。快乐的编码。
我解决了同样的问题
1.Use添加jar的正确方法
Right click your lib folder and choice "add library", and input to project.
- 在settings.gradle中添加。
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
- 编译环境:
- Android Studio 1.4
- 目标sdk:21
- jars:httpclient-4.3.6.jar,httpcore-4.3.3.jar,httpmime-4.3.6.jar