0

So I have this POST request made to the server and based on an argument the server will return error message within the errorBody() of Retrofit. I am trying to handle that Plain Text error returned by the server and then display it to the user within my Android application which uses Java. Below is my current attempt but this is giving me this error in Logcat:

@Url cannot be used with @POST URL (parameter #1)

Here is 400 response from the server:

enter image description here

Interface:

public interface ChangePickLocationClient
{

@GET
Call<ResponseBody> checkItem(@Url String url, @Header("Authorization") String authToken);

@GET
Call<String> getStringError(@Url String url, @Header("Authorization") String authToken);


@POST("Pick/ChangePickLocationAcceptChange")
Call<String> changePickLocationPOST(@Url String url, @Header("Authorization") String authToken, @Body 
ChangePickLocationPostModel changePickLocationPostModel);
}

Implementation:

private static final String BASE_URL = "http://00.00.00.1234/api/";
Gson mGson = new Gson();
Retrofit retrofit = new Retrofit.Builder().client(new OkHttpClient())
        .baseUrl(BASE_URL).addConverterFactory(ScalarsConverterFactory.create())
        .addConverterFactory(GsonConverterFactory.create(mGson))
        .build();
        
 ChangePickLocationClient ChangePickLocationClient = 
 retrofitPOST.create(ChangePickLocationClient.class);
 String itemNumber = itemNumberValue.getText().toString();
 newPickLocationValue.setText(newPickLocationValue.getText().toString().toUpperCase());
 String newPickLocation = newPickLocationValue.getText().toString();
 String token = globalClass.getActiveToken();
    
 final ChangePickLocationClient mChangePickLocationInterface = 
    retrofit.create(ChangePickLocationClient.class);
                    Call<String> mCallErrorPOST = mChangePickLocationInterface.changePickLocationPOST
                            (postUrl, "Bearer " + globalClass.getActiveToken(), 
                                                  changePickLocationPostModel);
                    call.enqueue(new Callback<ChangePickLocationPostModel>()
                    {
                        @Override
                        public void onResponse(Call<ChangePickLocationPostModel> call, 
     Response<ChangePickLocationPostModel> response)
                        {
                            String mPlainTextResponse = null;
                            try {
                                if(response.errorBody() != null)
                                {
                                    mPlainTextResponse = response.errorBody().string();
                                }

                            } catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            Toast.makeText(ChangePickLocation.this, mPlainTextResponse 
      ,Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailure(Call<ChangePickLocationPostModel> call, Throwable t)
                        {
                            Toast.makeText(ChangePickLocation.this, "Unknown server error!" 
       ,Toast.LENGTH_SHORT).show();
                        }
                    });
Reaper
  • 35
  • 5

1 Answers1

0

When the response is 400, the second call being made needs to be a clone() call. This is because the Call cannot be used more than once as stated in the documentation.

use this:

call.clone().enqueue(new Callback<ChangePickLocationPostModel>()

instead of

call.enqueue(new Callback<ChangePickLocationPostModel>()
Reaper
  • 35
  • 5
  • https://stackoverflow.com/questions/35093884/retrofit-illegalstateexception-already-executed – Reaper Sep 17 '20 at 11:10