什么外部JAR做我需要做一个Android多部分POST?做一个、部分、JAR、POST

2023-09-05 05:05:47 作者:一瞬间、心冷

我试图做一个Android的多部分POST,使用下面的code

I am trying to do a Android Multipart POST, using the following code

   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();
        }

这在

    MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

我想这是跟我使用的是外部JAR。我在我的项目中的libs文件夹中,我已经提到我的项目构建路径下的文件。

My guess this is to do with the external Jars I am using. I have a Libs folder in my project and I have referenced the following files in my project build path.

HttpClient的-4.2.jar,的HttpCore-4.2.jar和httpmime-4.2.jar

httpclient-4.2.jar, httpcore-4.2.jar and httpmime-4.2.jar

这些是正确的版本?或者我需要更多的JARS?

Are these the correct versions? Or do I need more JARS ?

推荐答案

在你的Andr​​oid项目,创建一个名为库在项目的根目录下(旁的src /,RES /等。)

In your Android project, create a directory named libs at the root of your project (next to src/, res/, etc.)

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

Locate the JAR file for the library you want to use and copy it into the libs/ directory.

设置项目以使用库:

更多参考这里: