1

I have method called SendPoints which enable users to send points to eachother and it's working. Now I need to retrieve an error message in JSON format from server if transaction is not successuful:

Those are important clases:

Service.java
@POST("/api/SendPoints")
Call<Void> SendPoints(@Body SendPoints SendPoints);

Network SDK class

NetworkSDK
  public void SendPoints (SendPoints SendPoints, Callback<Void> callback){
    Call<Void> call = BaseClient.getService().SendPoints(SendPoints);
    call.enqueue(callback);
}

This is object that I need to retrieve

public class ReturnMessage {

    private String Message;

    /**
     *
     * @return
     * The Message
     */
    public String getMessage() {
        return Message;
    }

    /**
     *
     * @param Message
     * The Message
     */
    public void setMessage(String Message) {
        this.Message = Message;
    }
}

and this is the call method:

NetworkSDK.getInstance().SendPoints(Posiljka, new Callback<Void>() {
                @Override
                public void onResponse(Call<Void> call, Response<Void> response) {
                    Log.d("Kartica", Posiljka.getCardNumber());
                    Log.d("Broj bodova", Posiljka.getAmount().toString());
                    Log.d("Response code:", "" + response.code());

                    if (response.code() == 200) {
                        Toast.makeText(getActivity(), "Points are sent !", Toast.LENGTH_SHORT).show();

                    }
                    else
                        Toast.makeText(getActivity(), "Points are not sent !", Toast.LENGTH_SHORT).show();

                    ****Here i want to get toast messages that server will return ( server will return strings (-1,-2,-3,-4 etc and I need to write message for**** 


                }

                @Override
                public void onFailure(Call<Void> call, Throwable t) {
                }
            });
        }catch (Exception e) {
            Toast.makeText(getActivity(), "Error !", Toast.LENGTH_SHORT).show();

        }  
    }
});
Chintan Desai
  • 2,607
  • 2
  • 22
  • 25

2 Answers2

0

Change this method

@POST("/api/SendPoints")
Call<Void> SendPoints(@Body SendPoints SendPoints);

You're expecting nothing by declaring Void

Change it to Call<String> if you expect a String from the server


Edit:

You can also get the HTTP Response header by doing this:

@Override
public void onFailure(Call<Void> call, Throwable throwable) {
    if (throwable instanceof HttpException) {
        ((HttpException) throwable).message(); // HTTP status message
        ((HttpException) throwable).code(); // HTTP status code
    }
}
Tom Sabel
  • 3,935
  • 33
  • 45
0

There are at least two ways of treating what you're asking.

  1. Throw an exception in method SendPoints and have that transformed by an ExceptionHandler [JAX-RS / Jersey how to customize error handling?)
  2. As stated by @Exaqt, you can declare a generic return type which comes with a status code.

Personally I might prefer the first aproach as it helps me structure the code and distribute the logic (I can declare the ExceptionHandler to return HttpStatus codes and a response body within).

Community
  • 1
  • 1
João Rebelo
  • 79
  • 14