1

我正在处理以下问题:当尝试通过 MultiPart Entity 上传图像时,似乎 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.jarhttpmime-4.2.jar库,但同样,它在我的注释行上崩溃了。

我错过了什么吗?

4

3 回答 3

4

好的,这里有 Multipart post 所需的库,所有学分都归功于Satya KomatineniDavid 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 内部)

于 2012-06-05T18:26:06.357 回答
1

I have the same problem and it was fixed by moving jars files into 'libs' directory in your android project.

于 2012-06-05T16:56:48.463 回答
0

确保在项目构建路径中检查了 jar。(项目 -> 属性)

于 2012-06-05T16:36:49.667 回答