背景: 我是android编程的新手。我想简单地向本地服务器发送一个http get请求。
我想将此请求作为参数传递给该请求,并希望在json中获得返回。这个问题我无法在主线程上执行它。我怎么能这样做?
以下是我的尝试:
主要课程:
itemsAdapter.add(get.getName(device.getName()));
在同一档案中单独分类:
private class httpGet extends AsyncTask<Editable, Void, Integer> {
protected String doInBackground(Editable... params) {
Editable editable = params[0];
return getName(editable.toString());
}
final String getName(String btName) {
HttpResponse response = null;
String result = "";
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
URI website = new URI("http://192.168.1.105/getName.php?q=" + btName);
request.setURI(website);
response = client.execute(request);
// Convert String to json object
JSONObject json = new JSONObject(response.toString());
// get LL json object
JSONObject json_Name = json.getJSONObject("Name");
// get value from LL Json Object
name = json_Name.getString("value"); //<< get value here
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
// Do something to recover ... or kill the app.
}
return result;
}
protected void onPostExecute(Integer result) {
// here you have the result
}
我不确定这是否是完成此任务的好方法。我也不知道我怎么称呼它。
答案 0 :(得分:1)
您应该了解asyncTask的工作原理。在DoInBackground中,您应该将代码引用到HTTPRequest。我建议使用方法来提高对代码的理解。以下是我的某个应用的示例:
public String query(String uri) {
HttpClient cliente = new DefaultHttpClient();
HttpContext contexto = new BasicHttpContext();
HttpPost httpPost = new HttpPost(uri);
HttpResponse response = null;
String resultado=null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("dato", cod_restaurante));
httpPost.setEntity(new UrlEncodedFormEntity(params));
response = cliente.execute(httpPost, contexto);
HttpEntity entity = response.getEntity();
resultado = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
// TODO: handle exception
}
return resultado;
}
private class MyAsyncTask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params)
{
result=query(params[0]);
return result;
}
protected void onPostExecute(final String resultadoDoInBackground)
{
//here put the code to modify the UI
}
}
然后在您的活动onCreate()方法中执行Asynktask。
new MyAsyncTask().execute(" ");
您可以在此处阅读有关AsyncTask的更多信息: AsyncTask Android Developer
答案 1 :(得分:1)
AsyncTask允许您在不同的线程中执行后台操作,而无需操作线程/处理程序。
应该是这样的:
private class httpGet extends AsyncTask<ParamForDoInBackground, ParamForOnProgressUpdate, ParamForOnPostExecute> {
protected Long doInBackground(ParamForDoInBackground... urls) {
// do the request here
}
protected void onProgressUpdate(ParamForOnProgressUpdate progress) {
// if you need to show any progress of the
// request from doInBackground
}
protected void onPostExecute(ParamForOnPostExecute result) {
// this method will run when doInBackground
// is done executing
}
}
然后你可以执行AsyncTask:
new httpGet().execute(ParamForDoInBackground);
您可以将以下内容用作参考:AndroidBackgroundProcessing和Android Developer AsyncTask