1

我像这样向服务器发送请求

public HttpResponse post(String URN, String entity) throws IOException {
    HttpPost httpPost = new HttpPost(URL + URN);
    httpPost.setHeader("Content-type", "application/json");
    httpPost.setHeader("Accept", "application/json");
    httpPost.setEntity(new StringEntity(entity, HTTP.UTF_8));
    return httpClient.execute(httpPost, httpContext);
}

要创建一个新帐户,电话号码会发送到服务器并进行验证。如果数字无效,则返回 400 BAD REQUEST 错误。

在来自服务器的响应中,内容中会返回一个 JSON 对象。这包含返回 400 错误的原因的详细信息。典型的 JSON 响应如下所示

{"dev_message": "The number 93015205229804 isn't a valid mobile number.", "user_message": "Invalid phone number", "error_code": 108}

当返回 400 错误时,execute 方法会引发 IOException。如果自动抛出错误,我该如何从 HttpResponse 获取消息?

4

0 回答 0