我目前正在开发一款Android应用程序,允许用户将图像上传到我的服务器。我是android开发的新手,因此目前面临在使用Http MultiPartEntity时将文件发送到服务器的问题
以下是我用于将文件上传到服务器的代码 私有类UploadFileToServer扩展了AsyncTask {
@Override
protected String doInBackground(Void... params) {
return uploadFile();
}
@SuppressWarnings("deprecation")
private String uploadFile() {
String responseString = null;
//Change URL here
String FILE_UPLOAD_URL = SERVER_SUBMIT_URL;
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(FILE_UPLOAD_URL);
MultipartEntity reqEntity = new MultipartEntity();
File file1 = new File(filePath1);
FileBody bin1 = new FileBody(file1);
reqEntity.addPart("image1", bin1);
//Extras
reqEntity.addPart("command", new StringBody("uploadImages"));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
resEntity = response.getEntity();
responseString = EntityUtils.toString(resEntity);
return responseString;
} catch (Exception e) {
Log.v(TAG, e.toString());
responseString = e.toString();
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
//Assuming JSON response From server
// {"status": true, "ideaID" : 1}
try{
JSONObject json = new JSONObject(result);
boolean status = json.getBoolean("status");
if(status){
showToast("Successfully Uploaded Images");
}else{
showToast("Image Uploading Failed");
}
}catch (Exception e) {
showToast("Image Uploading Failed");
Log.e(TAG, "Response from server: " + result + " " + e.getMessage());
}
// showing the server response in an alert dialog
super.onPostExecute(result);
}
}
我认为行“HttpResponse response = client.execute(post);”是应用程序崩溃的地方。
以下是日志中显示的错误。
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway /AndroidRuntime: FATAL EXCEPTION: AsyncTask #5
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:239)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at java.lang.Thread.run(Thread.java:838)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: Caused by: java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at org.apache.http.entity.ContentType.toString(ContentType.java:153)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at org.apache.http.entity.mime.MultipartFormEntity.<init>(MultipartFormEntity.java:53)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:236)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at org.apache.http.entity.mime.MultipartEntity.getEntity(MultipartEntity.java:119)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at org.apache.http.entity.mime.MultipartEntity.isChunked(MultipartEntity.java:138)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at org.apache.http.protocol.RequestContent.process(RequestContent.java:79)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at org.apache.http.protocol.BasicHttpProcessor.process(BasicHttpProcessor.java:290)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:167)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:413)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at com.example.grow.dubaiway.ImageActivity$UploadFileToServer.uploadFile(ImageActivity.java:197)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at com.example.grow.dubaiway.ImageActivity$UploadFileToServer.doInBackground(ImageActivity.java:156)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at com.example.grow.dubaiway.ImageActivity$UploadFileToServer.doInBackground(ImageActivity.java:137)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-22 00:24:41.341 8203-11792/com.example.grow.dubaiway E/AndroidRuntime: at java.lang.Thread.run(Thread.java:838)
真的可以在这个问题上使用一些帮助。 我查过了其他帖子,但它们对我不起作用。
我已经在apache网站上包含了以下库。 HttpClient的-4.5.1.jar HttpClient的缓存-4.5.1.jar 的HttpCore-4.4.3.jar httpmime-4.5.1.jar
这就是我的gradle文件的样子 apply plugin:'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.neeraj.imageapp"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
}
任何帮助将不胜感激。感谢
答案 0 :(得分:-1)
您可以尝试使用MultipartEntityBuilder
try
{
File file1 = new File(filePath1);
HttpClient client = new DefaultHttpClient();
MultipartEntityBuilder entBuilder = MultipartEntityBuilder.create();
entBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entBuilder.addBinaryBody(file1.getName(), file1);
//Extras
entBuilder.addPart("command", new StringBody("uploadImages"));
/*
If you need to send more files...
entBuilder.addBinaryBody(file2.getName(), file2);
entBuilder.addBinaryBody(file3.getName(), file3);
...
*/
HttpEntity entity = entBuilder.build();
HttpPost post = new HttpPost(FILE_UPLOAD_URL);
post.setEntity(entity);
HttpResponse response = client.execute(post);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
}
catch(Exception e)
{
e.printStackTrace();
}
您需要在gradle中添加依赖项
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5'
compile (group: 'org.apache.httpcomponents' , name: 'httpmime' , version: '4.3.5') {
exclude module: 'org.apache.httpcomponents:httpclient'
}
但我认为,当Google从Android 6(api 23)中删除它时,最好从代码中删除http apache依赖项(如HttpClient)
Android 6.0版本删除了对Apache HTTP客户端的支持。如果 您的应用正在使用此客户端并定位到Android 2.3(API级别9)或 更高,请改用HttpURLConnection类。这个API更多 高效,因为它通过透明减少网络使用 压缩和响应缓存,并最大限度地降低功耗