嘿,我在我的应用程序中使用HttpUrlConnection。而且在我看来,每当我打电话给.getInputStream()
或urlConnection.getResponseCode()
等时,它都会发出另一个请求,所以当我发出POST请求时,这对我来说并不好。有没有办法获得某种响应对象,它封装了响应数据,可以从UI线程访问,如下所示:
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?
}
}
答案 0 :(得分:0)
您可以执行以下步骤:
onReceive()
BroadcastReceiver
方法在UI线程上运行,因此您可以在那里快速更新您的用户界面。实施之后,您可以在注册到将从单身人士发送的意图后立即从您的活动中调用单件web服务调用方法...