2016-06-27 3 views
2
Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.Q_BASE) 
      .addConverterFactory(GsonConverterFactory.create()).client(client).build(); 
    Api.ApiInter inter = retrofit.create(Api.ApiInter.class); 
    Call<JSONObject> call = inter.getMovieData(sortType[0], Api.P_KEY); 
    call.enqueue(new Callback<JSONObject>() { 
     @Override 
     public void onResponse(Call<JSONObject> call, Response<JSONObject> response) { 
      /* */ 
     } 

     @Override 
     public void onFailure(Call<JSONObject> call, Throwable t) { 
      Log.d(LOG_TAG, "failed to get response."); 
     } 
    }); 

/retrofit2. 응답 본문은 "} {"

public class Api { 
    public static final String Q_BASE = "http://api.themoviedb.org/"; 
    public static final String Q_KEY = "api_key"; 
    public static final String Q_RESULT = "results"; 
    public static final String Q_POSTER_PATH = "poster_path"; 
    public static final String Q_TITLE = "title"; 
    public static final String Q_OVERVIEW = "overview"; 
    public static final String Q_DATE = "release_date"; 
    public static final String Q_VOTE_AVERAGE = "vote_average"; 
    public static final String P_KEY = "This is private key"; 
    public static final String REQUEST = "GET"; 

    public static final String I_BASE = "http://image.tmdb.org/"; 
    public static final String P_POSTER_SIZE = "w185"; 

    public interface ApiInter { 
     @GET("3/movie/{sort}") 
     Call<JSONObject> getMovieData(@Path("sort") String sort, @Query("api_key") String key); 

     @GET("t/p/{size}/{url}") 
     Call<Bitmap> getMoviePoster(@Path("size") String size, @Path("url") String url); 
    } 
} 

내 응용 프로그램을 디버깅 [Debugger]

, response.body()는 항상에만 retruned null입니다. 그리고 Call 요소를 변경하면 반환 값은 null이됩니다. 나는 그 이유를 모른다. JSON 데이터를 가져오고 싶습니다. 내가 어떻게 고쳐? okhttp 클라이언트를 설정할 수 있습니까? 또는 통화의 반환 유형을 변경 하시겠습니까? 제게 답을주십시오.

+0

당신은 백 밴드에 데이터가없는 것처럼 보입니다. –

답변

5

아주 간단한 실수입니다. 나 같은 문제가 있으면 3 가지를 확인하십시오.

  1. 개조 변환기는 Gson입니다.
  2. 귀하의 API 인터페이스 방법은 "{}"

개조가 GsonConverter 사용() 그냥 Call<JSONObjcet>

  • 요청이 성공하지만 response.body입니다 반환합니다. 따라서 com.google.gson 패키지에 jsonObject를 사용해야합니다. 변경해야합니다 JSONObjectJsonObject

  • +0

    고마워요. 내 문제를 해결했습니다. –

    +0

    고맙습니다. – gautam

    0

    응답 코드는 이미지에서 요청이 성공했음을 의미하는 200입니다. 그래서 서버가 {}을 반환합니다. 어쩌면 매개 변수로 전달하는 sortType과 관련된 데이터가 없을 수도 있습니다.

    +0

    요청 URL을 확인했지만 잘못되었습니다. 나는 문제가 끊임없이 또는 url에 없다고 생각한다. –

    관련 문제