Reputation: 17131
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?
Upvotes: 0
Views: 468
Reputation: 17131
Can read this error:
Response response=okHttpClient.newCall(request).execute();
if (response.code() != 200) {
errorMessage= response.body().string();
}
Upvotes: 0
Reputation: 4646
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
Upvotes: 1