만약 당신의 질문을 이해한다면, 네트워크 연결이없는 경우를 대비하여 SQLite로부터 데이터를 가져오고 싶습니다.
public interface GetCommentsService {
void getComments(int id, CommentsServiceResultListener listener);
}
CommentsServiceResultListener가 있습니다 :
public interface CommentsService {
@GET("posts/{postId}/comments")
Call<List<Comment>> getCommentsOfPost(@Path("postId") int id);
}
그런 다음 당신은 당신이 응용 프로그램의 다른 부분에서 사용하는 다른 인터페이스를 가지고해야합니다
당신은 당신이 당신의 엔드 포인트를 정의하는이 인터페이스를 가지고 리스너를 사용하여 이전 인터페이스의 구현으로 전달합니다. 다음과 같이 현실을 정의 할 수 있습니다 :
public interface CommentsServiceResultListener {
void onResponse(List<Comment> response);
void onError(String errorMessage);
}
, 당신은 실제로 데이터를 얻기 위해, 당신의 GetCommensService 인터페이스를 구현해야합니다.다음과 같이 할 수 있습니다.
public class GetCommensServiceImpl implemens GetCommensService {
private static final String TAG = BuildingsBaseServiceImpl.class.getSimpleName();
@Override
public void getComments(int id, CommentsServiceResultListener listener) {
CommentsService service = getService();
Call<List<Comment>> request = service.getCommentsOfPost(id);
request.enqueue(new Callback<List<Comment>>(){
@Override //if this method is executed, the actual call has been made
public void onResponse(Call<List<Comment>> call, Response<List<Comment>> response) {
if (response.isSuccessful()) {
listener.onResponse(response.body());
} else {
//TODO check here if the call wans't successful because a network problem. In that case, fetch from your SQLite
//Default unsuccessful call management
Log.e(TAG, response.code() + ": " + response.message());
listener.onError(response.message());
}
}
@Override //maybe the call couldn't be made because of lack of connection.
public void onFailure(Call<List<Comment>> call, Throwable t) {
//TODO check the failure cause, then decide if there's need to fetch from your SQLite.
//Default failure management
Log.e(TAG, t.getMessage() + "", t);
listener.onError(t.getMessage() + "");
}
});
}
private CommentsService getService() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://your.base.url/")
.addConverterFactory(GsonConverterFactory.create()) //assuming JSON
.build();
return retrofit.create(CommentsService.class);
}
}
희망이 있습니다.
죄송합니다. 질문이 정확하지 않습니다. –