У меня есть следующий код, чтобы сделать сообщение на URL-адрес, чтобы получить ответ как String. Но я хотел бы получить также код ответа HTTP (404,503 и т. Д.). Где я могу его восстановить? Я пробовал методы, предлагаемые классом HttpReponse, но не нашел его.Получить http-код ответа в java
Благодаря
public static String post(String url, List<BasicNameValuePair> postvalues) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
if ((postvalues == null)) {
postvalues = new ArrayList<BasicNameValuePair>();
}
httppost.setEntity(new UrlEncodedFormEntity(postvalues, "UTF-8"));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
return requestToString(response);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static String requestToString(HttpResponse response) {
String result = "";
try {
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line + "\n");
}
in.close();
result = str.toString();
} catch (Exception ex) {
result = "Error";
}
return result;
}
'response.getStatusLine() getStatusCode()' сразу после 'httpclient.execute' так что если код' HttpStatus.SC_OK' вы звоните 'requestToString' в других случаях у вас есть ошибка :) – Selvin
@Selvin - как насчет ответа на этот комментарий? Это правильно кстати, +1. – Perception
http://stackoverflow.com/questions/2592843/android-how-get-the-status-code-of-an-httpclient-request – Muse