我正在尝试使用以下代码进行Android Multipart POST
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlString);
File file = new File(pic);
Log.d(TAG, "UPLOAD: setting up multipart entity");
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
Log.d(TAG, "UPLOAD: file length = " + file.length());
Log.d(TAG, "UPLOAD: file exist = " + file.exists());
try {
mpEntity.addPart("datafile", new FileBody(file, "application/octet"));
mpEntity.addPart("id", new StringBody("1"));
} catch (UnsupportedEncodingException e1) {
Log.d(TAG, "UPLOAD: UnsupportedEncodingException");
e1.printStackTrace();
}
httppost.setEntity(mpEntity);
Log.d(TAG, "UPLOAD: executing request: " + httppost.getRequestLine());
Log.d(TAG, "UPLOAD: request: " + httppost.getEntity().getContentType().toString());
HttpResponse response;
try {
Log.d(TAG, "UPLOAD: about to execute");
response = httpclient.execute(httppost);
Log.d(TAG, "UPLOAD: executed");
HttpEntity resEntity = response.getEntity();
Log.d(TAG, "UPLOAD: respose code: " + response.getStatusLine().toString());
if (resEntity != null) {
Log.d(TAG, "UPLOAD: " + EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
这与
中的“java.lang.noclassdeffounderror org.apache.http.entity.mime.multipartentity”崩溃 MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
我的猜测这与我正在使用的外部罐子有关。我的项目中有一个Libs文件夹,我在项目构建路径中引用了以下文件。
httpclient-4.2.jar,httpcore-4.2.jar和httpmime-4.2.jar
这些是正确的版本吗?或者我需要更多JARS?
答案 0 :(得分:0)
在Android项目中,在项目的根目录下创建一个名为libs的目录(在src /,res /等旁边)
找到要使用的库的JAR文件,并将其复制到libs /目录中。