在 Android 中显示 httpGet 和响应之间的进度图像
Showing progress image between httpGet and response in Android
我使用下面的代码从 Web 服务获取 XML 文件:
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet HttpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(HttpGet);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
Web 服务响应请求大约需要 3-4 秒。
我想在请求和响应之间的这段时间内显示图像。
最好的方法是什么?
如有任何帮助,我们将不胜感激。
使用AsyncTask
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
// Show image Here
}
@Override
protected String doInBackground(String... params) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet HttpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(HttpGet);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
/Handle Result From server
}
@Override
protected void onProgressUpdate(Void... values) {
}
我使用下面的代码从 Web 服务获取 XML 文件:
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet HttpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(HttpGet);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
Web 服务响应请求大约需要 3-4 秒。 我想在请求和响应之间的这段时间内显示图像。
最好的方法是什么?
如有任何帮助,我们将不胜感激。
使用AsyncTask
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
// Show image Here
}
@Override
protected String doInBackground(String... params) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet HttpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(HttpGet);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
/Handle Result From server
}
@Override
protected void onProgressUpdate(Void... values) {
}