2012-10-12 1 views
8

loopj에서 멋진 비동기 http 라이브러리를 사용하고 있지만, 작은 걸림 소리가났습니다.Loopj Android Async Http - onFailure가 실행되지 않았습니다.

사용자가 인터넷에 연결되어 있지 않거나 연결이 끊어지면 앱에서 아무 것도 반환하지 않습니다. 이 부분은 예상되지만 onFailure 메서드는 실행되지 않습니다.

또한 인터넷 연결이있을 때 사용한 코드가 작동하므로 서버쪽에 문제가 없습니다.

다음은 최소한으로 제거 된 일부 코드입니다. 또한 (I이 너무 테스트 한)

String url = getString(R.string.baseurl) + "/appconnect.php"; 
client.getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); 
client.get(url, null, new JsonHttpResponseHandler() 
{ 
    @Override 
    public void onSuccess(JSONArray response) 
    { 
     Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onFailure(Throwable e, JSONArray errorResponse) 
    { 
     Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_SHORT).show(); 
    } 
}); 

감사를 작동하지 않습니다 애슐리

답변

7

당신이 시도 할 수 있습니다 : 당신이 다른onFailure 콜백을 구현하는 경우 그 중 하나가 발생한다

while (retry) { 
     try { 
      makeRequest(); 
      return; 
     } catch (UnknownHostException e) { 
      if(responseHandler != null) { 
       responseHandler.sendFailureMessage(e, "can't resolve host"); 
      } 
      return; 
     } catch (SocketException e){ 
      // Added to detect no connection. 
      if(responseHandler != null) { 
       responseHandler.sendFailureMessage(e, "can't resolve host"); 
      } 
      return; 
     } catch (IOException e) { 
      cause = e; 
      retry = retryHandler.retryRequest(cause, ++executionCount, context); 
     } catch (NullPointerException e) { 
      // there's a bug in HttpClient 4.0.x that on some occasions causes 
      // DefaultRequestExecutor to throw an NPE, see 
      // http://code.google.com/p/android/issues/detail?id=5255 
      cause = new IOException("NPE in HttpClient" + e.getMessage()); 
      retry = retryHandler.retryRequest(cause, ++executionCount, context); 
     } 
    } 
+0

이것은 해결책이다! loopj에 의해 [병합되었습니다] (https://github.com/loopj/android-async-http/commit/f854f62633726ab38d2795d0c1e805212a4f0d76)! 좋은 사람은 멋지다! – dbro

+0

도와 드릴 수있어서 다행입니다 – nicous

+0

안녕하세요. AsyncHttpRequest.class를 어떻게 편집 할 수 있습니까? 이 .class 파일을 jar 파일 내부에서 편집 할 수있는 방법이 있습니까? –

7

은 그래, 불행하게도 loopj 안드로이드 라이브러리는 잘 설계되지 않았습니다. , AsyncHttpRequest->makeRequestWithRetries()에서

같은 SocketException에 캐치를 추가 :

@Override 
public void onFailure(Throwable e) { 
    Log.e(TAG, "OnFailure!", e); 
} 
@Override 
public void onFailure(Throwable e, String response) { 
    Log.e(TAG, "OnFailure!", e); 
} 
@Override 
public void onFailure(Throwable e, JSONArray errorResponse) { 
    Log.e(TAG, "OnFailure!", e); 
} 
+1

이것들과 JSONObject 실패를 시도했습니다. 불행히도 여전히 작동하지 않습니다. –

+0

나를 위해 일하지 않는다 어느 쪽도 –

0

시도 이 :

@Override 
protected Object parseResponse(byte[] responseBody) throws JSONException { 
    return super.parseResponse(responseBody); 
}