你能告诉我如何使用如何使用asynctask android调用json webservice来调用json web服务。我这样调用并获得响应。但我需要这样做才能调用我需要显示“please wait”在后台执行一些异步任务。并在响应到来时删除弹出的内容。
public static String getMethod(String url) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(url));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else {
// Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
// TODO Handle problems..
} catch (IOException e) {
// TODO Handle problems..
}
return responseString;
}
WebServiceRequestManager wb=new WebServiceRequestManager();
String ss= wb.getMethod("htoard?crsCode=add");
System.out.println("String"+ss);
答案 0 :(得分:2)
我的建议是使用AndroidAsyncHttp(LoopJ),原因如下
1)代码结构小巧,优雅
2)易于使用。
3)性能,真的很快
答案 1 :(得分:0)
这样做
这是您的 WebServiceRequestManager.java
public class WebServiceRequestManager {
public String getMethod(String url) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(url));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else {
// Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
// TODO Handle problems..
} catch (IOException e) {
// TODO Handle problems..
}
return responseString;
}
}
如何从asynctask的Activity中调用它?
public class MainActivity extends Activity {
private ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new AsyncTask<Void, Void, String>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
showProgressDialog("Please Wait...");
}
@Override
protected String doInBackground(Void... params) {
WebServiceRequestManager manager = new WebServiceRequestManager();
return manager.getMethod("http://50.57.145.165:8080/FirstGroupRailApps/jservices/rest/a/destinationdashboard?crsCode=add");
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out.println("Respose : " + result);
hideProgressDialog();
}
}.execute();
}
public void showProgressDialog(final String msg) {
runOnUiThread(new Runnable() {
public void run() {
if (progress == null || !progress.isShowing()) {
progress = ProgressDialog.show(MainActivity.this, "", msg);
}
}
});
}
public void hideProgressDialog() {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
if (progress.isShowing())
progress.dismiss();
} catch (Throwable e) {
}
}
});
}
}