0

我正在尝试使用以下代码进行 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 吗?

4

1 回答 1

0

在您的 Android 项目中,在项目的根目录下创建一个名为 libs 的目录(在 src/、res/ 等旁边)

找到要使用的库的 JAR 文件并将其复制到 libs/ 目录中。

设置项目以使用库:

更多参考这里:

于 2012-09-27T11:02:05.587 回答