通过Web API在Android中进行异常处理 [英] Exception Handling In Android from Web API

查看:129
本文介绍了通过Web API在Android中进行异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC API C#中的代码如下:

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");
    }

PostMan的结果

Result in PostMan

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

我在android应用程序上捕获了此错误消息.我使用的是okhttp3.

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();

如何在Android应用程序上捕获此消息?

How do to catch this message on the Android application?

推荐答案

可以读取此错误:

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

这篇关于通过Web API在Android中进行异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆