0

My code in MVC API C# this:

    [System.Web.Http.HttpPost]
    [System.Web.Http.Route("api/ServiceV1/Test")]
    public IHttpActionResult Test()
    {
        return BadRequest("Catch this message in Android application");
    }

Result in PostMan

{
    "Message": "Catch this message in Android application"
}

I catch this error message on android application. I used okhttp3.

    String MIME_JSON = "application/json";
    Gson gson = new Gson();
    RequestBody body = RequestBody.create(MediaType.parse(MIME_JSON), gson.toJson(object));

    Request request = new Request.Builder()
            .url(baseUrl + route)
            .post(body)
            .build();

    okHttpClient.newCall(request).execute();

How do to catch this message on the Android application?

0

2 Answers 2

1

the Call.execute() method returns a Response object, which has the code() method to find the status code, and the isSuccessful() method to find whether the code is in the [200,300) range, which generally represent success.

Generally looking at the documentation of libraries you are working with is helpful. Here's the documentation for okhttp

Sign up to request clarification or add additional context in comments.

Comments

0

Can read this error:

Response response=okHttpClient.newCall(request).execute();
if (response.code() != 200) {
  errorMessage= response.body().string();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.