4

I am finally getting a response from the Instagram's API for the access token.

final Call<TokenResponse> accessToken =  ServiceManager.createTokenService().getAccessToken(request);
accessToken.enqueue(new Callback<TokenResponse>() {
    @Override
    public void onResponse(Call<TokenResponse> call, Response<TokenResponse> response) {
        mtext.setText("hello");
      }

    @Override
    public void onFailure(Call<TokenResponse> call, Throwable t) {
        mtext.setText("failure");
    }
});

However when I check if response.isSuccess() it fails? I don't understand this.

Also, I am using Retrofit 2 and I have imported the Gson should it automatically map the data right?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Hasan Nagaria
  • 253
  • 6
  • 16
  • TokenRequest request = new TokenRequest(); request.setClient_id(Constants.CLIENT_ID); request.setClient_secret(Constants.CLIENT_SECRET); request.setGrant_type(Constants.AUTORISATION_CODE); request.setRedirect_uri(Constants.REDIRECT_URI); request.setCode(code); – Hasan Nagaria Feb 22 '16 at 02:56
  • I logged around a little bit and the error keeps saying you must provide client_id where as Im clearly putting that in. What could be the reason for this? – Hasan Nagaria Feb 22 '16 at 02:58

1 Answers1

9

In retrofit 2, onFailure only invoked when a network or unexpected exception occurred during the HTTP request.

response.isSuccess() when the http status is 2xx or 3xx it will return true, or else, it will return false.

So, when isSuccess is true, you can use right data model to map the json. You can define a common error model to map the json when isSuccess is false.

[EDIT]Add some smaple code snippet that I used retrofit2 in my project, hope this will help you.

@Override public void onResponse(Response<T> response, Retrofit retrofit) {
  onNetworkRequestFinished();
  if (response.isSuccess()) {
    onSuccess(response.body());
  } else {
    onFailed(response, retrofit);
  }
 }


@Override public void onSuccess(SignupResponse signupResponse) {
  ((JFragmentActivity) context).dialogHelper.dismissProgressDialog();
  sharedPreferenceHelper.saveLoginName(context, userName); 
  doAfterSignin(context, signupResponse);
}


public void onFailed(Response response, Retrofit retrofit) {
  Converter<ResponseBody, JErrorReponse> errorConverter =
    retrofit.responseConverter(JErrorReponse.class, new Annotation[0]);

    try {
      JErrorReponse error = errorConverter.convert(response.errorBody());
      if (error != null) {
        JToast.toastLong(error.msg);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

the JErrorResponse.java

public class JErrorReponse implements Serializable {
  public int status;
  public String msg;
  public Object err;
}

SignupResponse.java

public class SignupResponse extends BaseResponse {

  public UserLoginData data;
}

UserLoginData.java

public class UserLoginData implements Serializable {
  @SerializedName("u_id") public int uId;

  @SerializedName("username") public String userName;

  public String icon;
}
Weibo
  • 1,042
  • 8
  • 18
  • What do you mean by 2xx or 3xx and how can I achieve that? – Hasan Nagaria Feb 22 '16 at 02:34
  • Also, I have made the relevant pojo classes so once I do get true for onsuccess, how can map it onto those classes? I thought that retrofit 2+gson would do that automatically. – Hasan Nagaria Feb 22 '16 at 02:35
  • @Hasan Nagaria #1 When your api response with http status code 2xx or 3xx, such as 200 or 304, response.isSuccess() will be return true. #2 You are right, retrofit will map the pojo class with json automatically. – Weibo Feb 22 '16 at 02:41
  • You are correct, I just checked and im getting 4xx which means Im doing something wrong. – Hasan Nagaria Feb 22 '16 at 02:44
  • 1
    Yes, 4xx means something incorrect in you request parameter, header, etc., you will get the callback still on ```onResponse(Response response, Retrofit retrofit) ```, but the response.isScucess will return false in this case. BTW, I just added some more sample code. – Weibo Feb 22 '16 at 03:06
  • This looks good, ill definitely use this in my code as well! Thanks – Hasan Nagaria Feb 22 '16 at 03:36
  • Please see my additional post, I am having a similar issue: http://stackoverflow.com/questions/39261661/null-responses-using-retrofit-calls – tccpg288 Sep 01 '16 at 02:39