2014-10-29 3 views
1

Robospice에서 제공하는 Retrofit 모듈을 사용하고 있습니다. 비동기 개조 호출을하고 있습니다. 문제는 내 호출이 성공적이지 않으면 Retrofit Error가 발생하지만 robospice에서는 onSuccess가 아닌 onFailure (RobospiceException)가 피드백을받습니다.RetrofitError에서 캐치 오류가 발생하여 RoboSpice 오류로 전달합니다.

다음 코드를 사용하여 웹 서비스 호출을 실행하고 있습니다.

mSpiceManager.execute(mProfileRequest, new ProfileRequestListener()); 

다음과 같이 ProfileRequest의 loaddatafromnetwork 호출은 다음과 같습니다

@Override 
public Profile loadDataFromNetwork() throws Exception { 
    initCountDownLatch(); 
    //Adding a retrofit callback. 
    getService().getProfile("//profileid", new AbstractCallback<Profile>() { 
     @Override 
     public void success(Profile profile, Response response) { 
      super.success(profile, response); 
      ProfileRequest.this.profile = profile; 
      mCountDownLatch.countDown(); 
     } 

     @Override 
     public void failure(RetrofitError retrofitError) { 
      super.failure(retrofitError); 
      mCountDownLatch.countDown(); 
     } 
    }); 
    mCountDownLatch.await(); 
    return profile; 
} 

사람이 나를 Robospice은 onFailure를 얻을 볼 수있는 방법을 알려 수 있습니까? 간단하게 말하면 사람이

답변

1

당신이 실패의 RetrofitError()를 던져해야한다 (개조 콜백 구현으로 ")? 개조"비동기 호출 "을 robospice을 implmenting 노력하고 있습니다.

+0

어떻게 던집니까? @Override에서 Retrofit Error를 catch하는 방법 public void onFailure (최종 SpiceException spiceException)? – SoH

1

내가 Snicolas의 대답 @ 댓글을하려고했지만 나는 포인트를 가지고 있지 않았다.

@Snicolas가 말하는 것은 당신이 예외를 던지면 RoboSpice가 그것을 잡아 SpiceException 내부에 패킹 할 것이라고 생각한다. 당신은 다음과 같은 방법으로 RetrofitError에 접근 할 수 있어야한다. getCause() 방법 :

mySpiceException.getCause()

하고이 같은 RetrofitError를 던지는 시도 할 수 있습니다 :

또한
@Override 
public Profile loadDataFromNetwork() throws Exception { 
    initCountDownLatch(); 
    RetrofitError myRetrofitError = null; 
    //Adding a retrofit callback. 
    getService().getProfile("//profileid", new AbstractCallback<Profile>() { 
     @Override 
     public void success(Profile profile, Response response) { 
      super.success(profile, response); 
      ProfileRequest.this.profile = profile; 
      mCountDownLatch.countDown(); 
     } 

     @Override 
     public void failure(RetrofitError retrofitError) { 
      super.failure(retrofitError); 
      mCountDownLatch.countDown(); 
      myRetrofitError = retrofitError; 
     } 
    }); 
    mCountDownLatch.await(); 
    if (null != myRetrofitError) { 
     throw myRetrofitError; 
    } 
    return profile; 
} 

, 왜 당신이 개조 호출의 콜백 양식을 사용하고 있습니까? 이 작업을 수행 할 수 있도록 개조 통화의 동기 버전이 있습니다

@Override 
public Profile loadDataFromNetwork() throws Exception { 
    Profile profile = getService().getProfile("//profileid"); 
    ProfileRequest.this.profile = profile; 
    return profile; 
} 

당신이 개조는 서버 응답을 역 직렬화하는 방법을 알고 귀하의 프로필 클래스에 GSON 주석을 사용하기 만하면됩니다. Retrofit은 자동으로 프로파일 인스턴스를 인스턴스화하고 구성합니다. 그럼 그냥 돌려 줄 수 있어요. Retrofit이이 프로세스에서 오류를 발생 시키면 예외가 throw됩니다 ... RetrofitError를 포착하지 못하기 때문에 앞에서 설명한 것처럼 RetrofitError가 튀어 SpiceException에 패키지화됩니다.

+0

MattC와 Stephan에게 감사드립니다. 이전에 Synchronous retrofit 메서드를 사용했습니다. 그러나 다운로드 속도를 계산해야하며 중심적인 방법으로이를 수행하길 원했습니다. Retrofit Response 개체는 본문 길이와 보내고받은 시간을 제공합니다. 그래서 다운로드 속도를 계산하기가 꽤 쉽습니다. 하지만 Synchronous retrofit calls에서 Response 메서드를 얻지 못합니까? 따라서 콜백을 변경했습니다. – SoH

+0

@SoH 당신은 실제로 당신이 원하는 것을 정확히 얻을 수 있습니다! 반환 유형이 Response 인 동기 메서드를 선언하면 Raw Retrofit Response 개체가 생성됩니다. – MattC