There is such a working code block for retrofit2

OkHttpClient okHttpClient = new OkHttpClient(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(BaseSpiceService.URL_SITE+"/") .addConverterFactory(GsonConverterFactory.create()) .client(okHttpClient) .build(); ServerConnectionService scs = retrofit.create(ServerConnectionService.class); baseResponse = scs.updateMe1(map, body).execute().body(); Log.e("UpdateMeRequest", "loadDataFromNetwork: baseResponse code = " +baseResponse.getCode() ); Log.e("UpdateMeRequest", "loadDataFromNetwork: baseResponse message = " +baseResponse.getMessage() ); 

How can I integrate 404 status processing from the server and from there the message code and the message itself?

Separately caused the body of the error using

 String s = scs.updateMe1(map, body).execute.errorBody(); 

then this line in JsonObject also got on keys. But then, in the absence of 404 status from the server, such code was not executed.

How can I combine my code block and error handling in one thread?

And it would be great if I could access the status of the server codes and the html address, which is usually stored in the variable retrofit - after starting the stream

The interface is:

 @Multipart @retrofit2.http.POST("v1/me") Call<BaseResponse> updateMe1(@retrofit2.http.QueryMap Map<String, String> stringMap, @Part MultipartBody.Part file); 
  • one
    The idea should be something like this: Responce<PespoceBody> response = scs.updateMe1(map, body).execute() and then watch what response is: if(response.isSuccessful()){respose.body();} else {response.errorBody()} - Yuriy SPb

1 answer 1

Usually, instead of executing on the same .execute().body() stream, .execute().body() use the callback .enqueue(mCallback) to call the load from the UI stream, and there is no need to independently determine which answer came.

It seems in your case an example would be like this:

 scs.updateMe1(map, body).enqueue(new Callback<T>() { @Override public void onResponse(Call<T> call, Response<T> response) { if (response.body() == null) { // тут ловятся такие ошибки, как 404 и 500 } else { // тут объект класса <T> успешно создался } } @Override public void onFailure(Call<T> call, Throwable t) { // тут ловятся непредвиденные исключения, // появившиеся во время создания запроса или обработки ответа } } 

In this example, the class <T> is the class of the object that you expect in a successful response, in your case it is BaseResponse .

If all the same there is a need to run a synchronous download in the same stream, then this action would look like this:

  Response response = scs.updateMe1(map, body).execute(); int code = response.code(); // статус код сервера if (response.isSuccessful()) { baseRespose = response.body(); } else { Log.e("UpdateMeRequest", "HTTP status code = " + code ); } 

Well, the whole thing you probably already wrapped in a try-catch in case of unforeseen situations.

  • enqueue is an asynchronous stream, and execute is synchronous. That is, with enqueue there will be no consistency. Is that correct? And I need to process the result of the query synchronously, it turns out I need an execute. If my judgments are not true, report - zayn1991
  • That's right, just from the passage can not see the reasons for this approach. Why not make a separate class that launches an asynchronous loader from a UI thread :) - Artilirium
  • I have a project with both retrofit1 and retrofit2, and a separate class that starts up the loader synchronously from the stream UI but on retrofit1, I’ll get to this, but not today =) - zayn1991
  • Well then updated the answer) - Artilirium
  • added interface to question Will this change your code for me, with my interface? - zayn1991