简体   繁体   中英

multipart request on Android goes to timeout

I Need to upload one file from my Android using multipart and tried with the below code but with no luck. I got a connection timeout exception and tried with different code having the same result.

    try
    {
        HttpClient client = new DefaultHttpClient();

        HttpPost post = new HttpPost(URL);

        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
        entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

        entityBuilder.addTextBody(USER_ID, "DFD");
        entityBuilder.addTextBody(NAME, "DFD");

        String filepath = Environment.getExternalStorageDirectory() + "/Download/myImage.jpg";

        Log.d(MULTIPART_TAG, filepath);
        File file = new File(filepath);
        if(file != null)
        {
            entityBuilder.addBinaryBody("IMAGE", file);
        }

        HttpEntity entity = entityBuilder.build();

        post.setEntity(entity);

        HttpResponse response = client.execute(post);

        HttpEntity httpEntity = response.getEntity();

        String result = EntityUtils.toString(httpEntity);

        Log.v("result", result);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

Here is the exception that I get:

08-06 17:49:03.006: W/System.err(24761): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)

I also tried with those solutions:

https://stackoverflow.com/a/19188010/1948785

and with the deprecated class MultipartEntity

My second question is what is the meaning of this warning (I dont know if it is related with my problem but I get it when performing the request):

08-06 17:45:53.461: W/dalvikvm(24761): VFY: unable to resolve static field 3008 (INSTANCE) in Lorg/apache/http/message/BasicHeaderValueParser;

The libs I am using are those:

  • httpclient-4.3.4.jar
  • httpcore-4.3.2.jar
  • httpmime-4.3.4.jar

Try this ,

    DefaultHttpClient httpClient = new DefaultHttpClient();
    // yourimagefile is your imagefile

    HttpPost httpPostRequest = new HttpPost(URL);
    // Try This
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    MultipartEntity mpEntity = new MultipartEntity();
    ContentBody cbFile = new FileBody(yourimagefile, "image/jpeg");
    mpEntity.addPart("file", cbFile); 
    httpPostRequest.setEntity(mpEntity);
    HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);

Try to set timeout duration of HttpClient.

int TIMEOUT_MILLISEC = 20000;  // = 20 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);

Check the size of the image. It might be that the image is too large and it is taking a long period of time to upload and the server is issuing a timeout on that connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM