Java Android如何在Exception中获取HTTPUrlConnection(POST)响应码和消息?
Java Android How to get HTTPUrlConnection (POST) response code and message in the Exception?
这是我的情况。我正在使用 POST 消息调用 Web 身份验证 API。成功后,它 return 是一个令牌。否则,它会 return 错误消息 (json) 指定原因(例如:"incorrect password", "account lock", etc
),响应代码 400, etc
。
从浏览器查看的示例错误:
因此,我设法 调用、获取并 return 成功消息,但在出现错误时却没有。下面是伪代码(我省略了多余的部分,主要关注'catch'范围):
HttpURLConnection conn = null; //Must define outside as null?
try {
...
byte[] postDataBytes = ...
URL url = new URL(urlLogin);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod( "POST" );
...
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
for (int c; (c = in.read()) >= 0;)
sb.append((char)c);
String response = sb.toString(); // Yeah I got this
return response;
}
catch(IOException ex){
//the variable ex doesn't tell anything about the response code
//neither the response message
//so i access the conn to get
//
try {
int responseCode = conn.getResponseCode();
return "code: " + responseCode; //somehow this line just doesn't work!
}
catch (Exception ex2){
}
}
详细一点,IOException
没有说明任何有关响应的信息。所以我访问 HttpURLConnection
变量。
conn.getResponseCode() does return 400
,
conn.getResponseMessage() does return "Bad Request"
,但不是来自服务器的错误 json 消息。
有什么想法吗?
编辑:
我只想知道,在 Exception/error 期间获得响应 code/message 的正确方法是什么。
getErrorStream()
of HttpUrlConnection
可能就是您要找的。
Reader in = new BufferedReader(new InputStreamReader(conn.getErrorStream(), "UTF-8"));
// Do you response parsing here.
这是我的情况。我正在使用 POST 消息调用 Web 身份验证 API。成功后,它 return 是一个令牌。否则,它会 return 错误消息 (json) 指定原因(例如:"incorrect password", "account lock", etc
),响应代码 400, etc
。
从浏览器查看的示例错误:
因此,我设法 调用、获取并 return 成功消息,但在出现错误时却没有。下面是伪代码(我省略了多余的部分,主要关注'catch'范围):
HttpURLConnection conn = null; //Must define outside as null?
try {
...
byte[] postDataBytes = ...
URL url = new URL(urlLogin);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod( "POST" );
...
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
for (int c; (c = in.read()) >= 0;)
sb.append((char)c);
String response = sb.toString(); // Yeah I got this
return response;
}
catch(IOException ex){
//the variable ex doesn't tell anything about the response code
//neither the response message
//so i access the conn to get
//
try {
int responseCode = conn.getResponseCode();
return "code: " + responseCode; //somehow this line just doesn't work!
}
catch (Exception ex2){
}
}
详细一点,IOException
没有说明任何有关响应的信息。所以我访问 HttpURLConnection
变量。
conn.getResponseCode() does return 400
,
conn.getResponseMessage() does return "Bad Request"
,但不是来自服务器的错误 json 消息。
有什么想法吗?
编辑:
我只想知道,在 Exception/error 期间获得响应 code/message 的正确方法是什么。
getErrorStream()
of HttpUrlConnection
可能就是您要找的。
Reader in = new BufferedReader(new InputStreamReader(conn.getErrorStream(), "UTF-8"));
// Do you response parsing here.