2012-01-28 4 views
0

우선 코드가 더러워 보일 경우 미안하지만 들여 쓰기를 시도했지만 더 많은 연습이 필요합니다.Asynctask의 HTTPRetryHandler

AsyncTask (doInBackground)에서 3 개의 HTTPS 요청을하고 있습니다.

모든 요청은 웹 사이트에서 데이터를 가져옵니다.

나는 모든 요청을 세 번 다시 시도하는 HTTPRetryHandler를 만들기 위해 고심 중이다. 나는 이미 example이라는 멋진 코드를 찾았지만, 이것을 내 코드에 적용하는 방법을 모르겠다.

도움을 주실 수 있으면 정말 고맙습니다. 감사합니다.

나는 모든 요청에 ​​대해 동일한 HttpClient를를 사용 doInBackground에서

public HttpClient getHttpClient() { 

DefaultHttpClient client = null; 

    ResponseCache.setDefault(null); 

try { 





KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
      trustStore.load(null, null); 
      SSLSocketFactory sf = new MySSLSocketFactory(trustStore); 
      sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

      // Setting up parameters 
      HttpParams params = new BasicHttpParams(); 
      params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30); // default 30 
      params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30)); // default 30 
      //params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false); 
      HttpProtocolParams.setUseExpectContinue(params, false); 
      HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
      HttpProtocolParams.setContentCharset(params, "UTF-8"); 

      // Setting timeout 
      HttpConnectionParams.setConnectionTimeout(params, 30000); 
      HttpConnectionParams.setSoTimeout(params, 30000); 

      // Registering schemes for both HTTP and HTTPS 
      SchemeRegistry registry = new SchemeRegistry(); 
      registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
      registry.register(new Scheme("https", sf, 443)); 

      // Creating thread safe client connection manager 
      ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); 

      // Creating HTTP client 
      client = new DefaultHttpClient(ccm, params); 

     } catch (Exception e) { 
      client = new DefaultHttpClient(); 
      Toast.makeText(AndroidLogin.this, "Problem with connection!", Toast.LENGTH_LONG).show(); 
     } 

     return client; 
    } 

예 전화 : 내 HttpClient를 다음 코드를 넣어 자신에 의해이 문제를 해결 할 수 있었다

/******* 2nd HTTP Request *******/ 
       try 
       { 
        HttpPost request1 = new HttpPost("some webpage"); 
        HttpResponse response1 = httpClient.execute(request1); 

        InputStream inputStreamActivity1 = response1.getEntity().getContent(); 
        BufferedReader reader1 = new BufferedReader(new InputStreamReader(inputStreamActivity1)); 
        StringBuilder sb1 = new StringBuilder(); 
        String line1 = null; 
        String lookUp1 = "</response>"; 

        while ((line1 = reader1.readLine()) != null) 
        { 
         sb1.append(line1); 
         if (line1.indexOf(lookUp1)!= -1) 
         { 
          System.out.println("Found: </response>"); 
          HttpRequest2 = "success"; 
         } 
        } 
        STRINGBUILD_REQUEST2 = sb1.toString(); 
        inputStreamActivity1.close(); 
        publishProgress(response1.toString().length()); 

       } catch(SocketTimeoutException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        Log.e("TAG: ", "SocketTimeoutException occured during HTTP request 2!"); 
       } 
       /************* END 2nd HTTP Request *****************/ 

답변

0

합니다.

as setUseExpectContinue는이 문제를 해결하지 못하는 것 같습니다.

HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() { 

      @Override 
      public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { 
       // TODO Auto-generated method stub 
       if(executionCount >= 10) { // Retry 10 times   
        return false;     
        } 
       if(exception instanceof SocketTimeoutException) 
       {      
        return true;     
        } 
       else if (exception instanceof ClientProtocolException) 
       {      
        return true;     
        } 
       return false;    
       } 
      }; 

     client.setHttpRequestRetryHandler(retryHandler); 
관련 문제