我正在处理以下问题:尝试通过MultiPart实体上传图像时,似乎HttpClient / MIME上的几个更新正在破解所有内容。我正在尝试以下代码,但它失败了NoClassDefFoundError
:
public static void executeMultipartPost(File image, ArrayList<Cookie> cookies, String myUrlToPost) {
try {
// my post instance
HttpPost httppost = new HttpPost(myUrlToPost);
// setting cookies for the connection session
if (cookies != null && cookies.size() > 0) {
String cookieString = "";
for (int i=0; i<cookies.size(); ++i) {
cookieString += cookies.get(i).getName()+"="+cookies.get(i).getValue()+";";
}
cookieString += "domain=" + BaseUrl + "; " + "path=/";
httppost.addHeader("Cookie", cookieString);
}
// creating the http client
HttpClient httpclient = new DefaultHttpClient();
// creating the multientity part [ERROR OCCURS IN THIS BELLOW LINE]
MultipartEntity multipartEntity = new MultipartEntity();
multipartEntity.addPart("photoupload", new FileBody(image));
httppost.setEntity(multipartEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
} catch (Exception e) {}
}
此方法完全可编译并使用httpclient-4.0.1.jar和httpmime-4.2.jar库,但同样,它会在评论行上崩溃。
我错过了什么吗?
答案 0 :(得分:4)
好的,here你需要为Multipart帖子提供库,所有学分归 Satya Komatineni 和 David Maclean ,Pro Android 3和现在4和我引用他们的书
要进行多部分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内部)
答案 1 :(得分:1)
我有同样的问题,通过将jar文件移动到你的android项目中的'libs'目录来修复它。
答案 2 :(得分:0)
确保在项目构建路径中检查jar。 (项目 - &gt;属性)