Java Android POST请求400响应代码引发异常
当我向服务器发送POST请求时,如果响应为200,我将得到JSON正文。但是,对于不成功的请求,服务器会发送400响应代码,但我的android代码会抛出FileNotFoundException。读取400响应和200响应之间有什么区别吗Java Android POST请求400响应代码引发异常,java,android,post,http-status-code-400,Java,Android,Post,Http Status Code 400,当我向服务器发送POST请求时,如果响应为200,我将得到JSON正文。但是,对于不成功的请求,服务器会发送400响应代码,但我的android代码会抛出FileNotFoundException。读取400响应和200响应之间有什么区别吗 StringBuffer responseBuilder = new StringBuffer(); String line = null; HttpURLConnection conn = null; OutputS
StringBuffer responseBuilder = new StringBuffer();
String line = null;
HttpURLConnection conn = null;
OutputStream out = null;
BufferedReader rd = null;
System.setProperty("http.keepAlive", "false");
try
{
conn = (HttpURLConnection) new URL(requestURL).openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.setConnectTimeout(NetworkConstants.CONNECTION_TIMEOUT);
conn.setReadTimeout(NetworkConstants.SOCKET_TIMEOUT);
out = conn.getOutputStream();
Writer writer = new OutputStreamWriter(out, "UTF-8");
String s = formatParams();
Log.d("-------------------------------------------------->", s);
writer.write(s);
writer.flush();
writer.close();
}
catch (Exception e)
{
}
finally
{
if (out != null)
{
try
{
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
try
{
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null)
{
responseBuilder.append(line);
if (!rd.ready())
{
break;
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (conn != null)
{
conn.disconnect();
}
}
String response = responseBuilder.toString();
Log.d("@@@@@@@@@@@@@@@@@@@@@@@@@@@", response);
return response;
亲切问候,如果响应代码不是200或2xx,请使用getErrorStream()而不是getInputStream()来解析json并显示后端提供的消息。为此使用
getErrorStream()
。从:
如果HTTP响应指示发生错误,getInputStream()
将抛出一个IOException
。使用getErrorStream()
读取错误响应。可以使用getHeaderFields()
以正常方式读取标题
示例代码:
httpURLConnection.connect();
int responseCode = httpURLConnection.getResponseCode();
if (responseCode >= 400 && responseCode <= 499) {
Log.e(TAG, "HTTPx Response: " + responseCode + " - " + httpURLConnection.getResponseMessage());
in = new BufferedInputStream(httpURLConnection.getErrorStream());
}
else {
in = new BufferedInputStream(httpURLConnection.getInputStream());
}
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
urlResponse.append(line);
}
httpURLConnection.connect();
int responseCode=httpURLConnection.getResponseCode();
如果(responseCode>=400&&responseCode我知道这个问题已经问了很长时间了,但是为了其他仍然有这种问题的人的利益,请注意,问题的另一个可能原因是使用“connection.getContent()”获取InputStream。
类似这样:
InputStream is = (InputStream) connection.getContent();
这可能会造成一种问题,超过399的响应代码根本不会被处理。
因此,建议直接使用getInputStream()和getErrorStream(),如前面的注释和以下示例所示:
HttpURLConnection connection = null;
BufferedReader bufferedReader = null;
try {
String urlString = "http://www.someurl.com";
URL url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream is;
int responseCode = connection.getResponseCode();
if (responseCode < HttpURLConnection.HTTP_BAD_REQUEST) {
is = connection.getInputStream();
} else {
is = connection.getErrorStream();
}
StringBuilder response = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(is));
String tempLine;
while ((tempLine = bufferedReader.readLine()) != null) {
response.append(tempLine);
}
String serverResponse = response.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
HttpURLConnection=null;
BufferedReader BufferedReader=null;
试一试{
字符串URL字符串=”http://www.someurl.com";
URL=新URL(URL字符串);
connection=(HttpURLConnection)url.openConnection();
connection.connect();
输入流为;
int responseCode=connection.getResponseCode();
if(responseCode
所以服务器会用400响应代码向我发送信息。我怎么读呢?这是非常基本的东西,伙计,在建立连接时只需检查响应代码,然后自己处理响应。例如,如果你最终得到的不是200,你应该为用户显示某种消息,而不要继续尝试重新发送d响应。我们的服务器的设计方式是,如果请求参数不正确,比如用户提供了错误的用户名或密码,则会生成400响应代码。在这种情况下,服务器发送json响应并生成错误消息。因此,在这种情况下,我也必须读取响应。我如何才能做到这一点?是的,ins除了读取输入流,您还可以解析ErrorStream(getErrorStream())中提供的json。对于httpURLConnection.getErrorStream()
,这是一个很好的答案+1,因为它有助于读取响应中的错误内容。。。