What type should be in ResponseObject, so that you can get any response and then display it do not worry?

I create the interface:

interface GitHubService { @GET("products") Call<ResObject> repoContributors(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://somepath.com/") .addConverterFactory(GsonConverterFactory.create()) .build(); } 

I fulfill the request:

 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { GitHubService gitHubService = GitHubService.retrofit.create(GitHubService.class); final Call<ResponseObject> call = gitHubService.repoContributors(); call.enqueue(new Callback<ResponseObject>() { @Override public void onResponse(Call<ResponseObject> call, Response<ResponseObject> response) { final TextView textView = (TextView) findViewById(R.id.textView); textView.setText(response.body().toString()); } @Override public void onFailure(Call<ResponseObject> call, Throwable t) { final TextView textView = (TextView) findViewById(R.id.textView); textView.setText("Something went wrong: " + t.getMessage()); } }); } }); 

}

    1 answer 1

    Try setting the JsonElement type JsonElement this :

     call.enqueue(new Callback<JsonElement>() {...}); 

    further in onResponse:

     JsonObject jsonObj = response.body().getAsJsonObject(); String strObj = response.body().toString(); 

    Another option is to add the Interceptor, in which you will receive an unreformed answer.

     public static class MyInterceptor implements Interceptor { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request request = chain.request(); Response response = chain.proceed(request); Log.d(TAG, "response.body().string(): " + response.body().string()); return response; } } 

    It should be added to OkHttpClient, which should be added to Retrofit:

     OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new MyInterceptor()) .build(); Retrofit retrofit = new Retrofit.Builder() .client(client) //остальное .build(); 
    • I haven’t tried the first one, but the second one works exactly - YuriySPb
    • The first one also works like a Swiss watch - Alexander