0
嘿,我在我的應用程序中使用HttpUrlConnection。而且在我看來,每當我撥打.getInputStream()
或urlConnection.getResponseCode()
等電話時,它會發出另一個請求,所以當我發出POST請求時,它對我不利。有沒有一種方法,以獲得某種響應對象它封裝響應數據,可以從UI線程,這樣的訪問:Android HttpUrlConnection響應對象
private class RegisterAsync extends AsyncTask<String, Void, HttpResponse> {
protected String doInBackground(String... strings) {
HttpURLConnection urlConnection = null;
String message = null;
try {
URL url = new URL(REGISTER_URL);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
out.write(strings[0].getBytes("UTF-8"));
out.flush();
out.close();
HttpResponse response = urlConnection.getResponse();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
return Response;
}
protected void onPostExecute(HttpResponse response) {
//Do some with response object: get status, headers, content etc?
}
}
你可以訪問'響應onpostExecute'這是調用UI線程 – Raghunandan
是的,我說的是有沒有像HttpResponse對象? HttpUrlConnection沒有方法調用getResponse() –
看看這裏,http://stackoverflow.com/questions/10684086/getting-the-response-body-of-httpresponse – QVDev