0

I would like to handle my response from server , but I don't know how JSON (from server) its looks like. So I tried to display response as String , but I cant do it. Is it possible to display response as String? And then handle the response correctly. thanks

(Retrofit 1.9)

 LoginService loginService = RetrofitClient.createService(LoginService.class); loginService.searchOffer(getToken(), offerSearch, new Callback<String>() { @Override public void success(String offerSearchRequestResource, Response response) { String responseFromSercer = response.getBody(); } @Override public void failure(RetrofitError error) { } }); 
2
  • 1
    Post your logcat/error you are getting when trying your code. Commented Aug 1, 2017 at 17:05
  • This is what Im getting ,"retrofit.client.Response@c7b4f16" How can I handle it? Thanks for qucik reply! Commented Aug 1, 2017 at 17:24

2 Answers 2

1

change your response model to

JSONObject (from Gson)

then in your

public void success(...){response.body.toString();} 

like this:

 Call<JsonObject> call = YOUR_API_INTERFACE().YOUR_METHOD(YOUR_PARAMS); call.enqueue(new Callback<JsonObject>() { @Override public void onResponse(Call<JsonObject> call, @NonNull Response<JsonObject> response) { if(response.isSuccessful()) { String responseFromSercer = response.body.toString(); } } @Override public void onFailure(Call<JsonObject> call, Throwable t) { ..... } }); 
Sign up to request clarification or add additional context in comments.

Comments

0

If you are sure the request runs successfully and there is a response back...use

System.out.println(response.getBody()); 

i'd also suggest you add Logging Interceptors here so that you can get a detailed view of all your API calls

10 Comments

I'm sending something like that : String responeFromServer = response.toString(); Log.e(LOG_TAG, "Response" + responeFromServer); And this is reposne: "retrofit.client.Response@c7b4f16" it that means something?
Try System.out as described above and show me the logcat response to it. I may also want to look at your server code to see how it handles the API call
In Android Studio i can only use Log.d to show logcat. When I compile the code I getting response in logcat like ""retrofit.client.Response@c7b4f16" String responeFromServer = response.getBody().toString(); Log.d(LOG_TAG, "Response" + responeFromServer);
Can i see the server code? and look at adding logging interceptors
This is my interface @POST("/offer/search") void searchOffer(@Header("x-auth-token")String header, @Body OfferSearchRequestResource offerSearch, Callback<String> offerSearchRequestResourceCallback); RetrofitClient: private static RestAdapter.Builder builder = new RestAdapter.Builder() .setEndpoint(API_BASE_URL) .setLogLevel(RestAdapter.LogLevel.FULL) .setClient(new OkClient(new OkHttpClient())); public static <S> S createService(Class<S> serviceClass) { return createService(serviceClass, null, null);
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.