簡體   English   中英

Android上的多部分請求超時

[英]multipart request on Android goes to timeout

我需要使用multipart從我的Android設備上載一個文件,並嘗試使用以下代碼,但沒有成功。 我有一個連接超時異常,並嘗試了具有相同結果的不同代碼。

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

這是我得到的例外:

08-06 17:49:03.006:W / System.err(24761):原因:libcore.io.ErrnoException:連接失敗:ETIMEDOUT(連接超時)

我還嘗試了以下解決方案:

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

以及已棄用的類MultipartEntity

我的第二個問題是此警告的含義是什么(我不知道它是否與我的問題有關,但是在執行請求時會得到它):

08-06 17:45:53.461:W / dalvikvm(24761):VFY:無法解析Lorg / apache / http / message / BasicHeaderValueParser中的靜態字段3008(INSTANCE);

我正在使用的庫是:

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

嘗試這個 ,

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

嘗試設置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);

檢查圖像的尺寸。 可能是圖像太大,並且上載時間很長,並且服務器在該連接上發出了超時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM