我正在使用eclipse在Studio中转换我的项目。我正在使用 httpmime 4.5.1 jar文件将图像多部分发送到服务器。这是我的code.it给了我NoClassDefFoundError:org.apache.http.Consts错误。
请帮帮我。
这是我的代码。
new AsyncTask<String, Integer, Void>() {
@Override
protected void onPreExecute() {
// setting progress bar to zero
progressBar.setProgress(0);
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... progress) {
// Making progress bar visible
// updating progress bar value
progressBar.setProgress(progress[0]);
txtPercentage.setText(progress[0] + "%");
}
@SuppressWarnings("deprecation")
@Override
protected Void doInBackground(String... params) {
for (i = 0; i < allPath.length; i++) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
txtCount.setText((i + 1) + "/"
+ allPath.length);
}
});
allPath[i] = selected.get(i).sdcardPath;
String responseString = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
Config.FILE_UPLOAD_URL);
try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
File sourceFile = new File(
compressImage(allPath[i]));
// Adding file data to http body
entity.addPart("tag", new StringBody(
"productAdd"));
// Adding file data to http body
entity.addPart("image",
new FileBody(sourceFile));
entity.addPart(
"seller_id",
new StringBody(
CommonUtilities
.getSellerId(getApplicationContext())));
entity.addPart(
"shop_id",
new StringBody(
CommonUtilities
.getCurentShopId(
getApplicationContext())
.toString()));
entity.addPart(
"product_pId",
new StringBody(
CommonUtilities
.getMaxProductId(
getApplicationContext())
.toString()));
entity.addPart("cat_id", new StringBody(cat_id));
entity.addPart("product_image", new StringBody(
allPath[i]));
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;
flagForError = true;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
flagForError = true;
} catch (IOException e) {
responseString = e.toString();
flagForError = true;
}
if (flagForError == false) {
updateAfterResponse(responseString);
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
uploadDialog.cancel();
dialogForError();
}
});
task.cancel(true);
break;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setProgress(0);
}
});
if (i == allPath.length - 1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
txtCount.setText("Done");
}
});
}
// return responseString;
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// showing the server response in an alert
// dialog
super.onPostExecute(result);
// if (i == (allPath.length - 1)) {
if (uploadDialog.isShowing()) {
uploadDialog.dismiss();
}
}
}.execute();
它给了我错误
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: Process: com.elsner.orderlite, PID: 10320
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: a`enter code here`t java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at java.lang.Thread.run(Thread.java:841)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: Caused by: java.lang.NoClassDefFoundError: org.apache.http.Consts
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at org.apache.http.entity.mime.content.StringBody.<init>(StringBody.java:147)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at com.elsner.sellerproduct.CustomGalleryActivity$2$1.doInBackground(CustomGalleryActivity.java:311)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at com.elsner.sellerproduct.CustomGalleryActivity$2$1.doInBackground(CustomGalleryActivity.java:221)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: at java.lang.Thread.run(Thread.java:841)
答案 0 :(得分:3)
在bundle.gradle中使用
compile('org.apache.httpcomponents:httpmime:4.3.6') {
exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
答案 1 :(得分:0)
要进行多部分POST调用,您需要再获得三个Apache 开源项目:Apache Commons IO,Mime4j和HttpMime。您可以 从以下网站下载这些项目:Commons IO: http://commons.apache.org/io/ Mime4j:http://james.apache.org/mime4j/ HttpMime:http://hc.apache.org/downloads.cgi(在HttpClient中)
答案 2 :(得分:0)
如果您使用的是sdk版本23,则必须添加到您的gradle:
android {
compileSdkVersion 23
...
useLibrary'org.apache.http.legacy'
...
}