这是从this post's Question继续。
我无法想象如何从主UI线程添加单独的线程来完成从服务器收集数据的工作。我之前从未做过线程,我认为我构造的类中的这个实例比我能找到的任何一个例子更先进。
我的课程的任何帮助和发布的修订版本都将不胜感激。
Thank_you!
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
setContentView(R.layout.list_view2);
/**
* Get the query string from last activity and pass it to this
* activity-----------------------------------------------------
*/
String p = null;
if (extras != null) {
p = extras.getString(PHP_KEY);
}
loadQuery(p);
}
void loadQuery(String p) {
String qO = getIntent().getStringExtra("QUERY_ORDER");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/App/php/" +
p + qO + ".php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
setListAdapter(new QueryAdapter(this, result));
}
请参阅下面的答案
答案 0 :(得分:7)
您应该只使用AsyncTask
这是一个关于如何使用它的好教程Tutorial
以下是下载网页并将结果返回主UI的示例。
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
textView.setText(result);
}
}
答案 1 :(得分:3)
使用AsyncTasks,Luke。
答案 2 :(得分:2)
我会使用AsyncTask的扩展。这是一个示例:
public TestSync extends AsyncTask<Void, Integer, Integer> {
TextView mTv = null;
String mURL;
public TestSync(TextView tv, String url) {
mTv = tv;
mURL = url;
}
@Override public Void doInBackground(Void... voids) {
int count = 0;
URL url = null;
BufferedReader br = null;
try {
url = new URL(mURL);
br = new BufferedReader(new InputStreamReader(url.openStream));
String log = null;
while ((log = br.readLine()) != null) {
Log.d("Testo", log);
count++;
publishProgress(new int[] {count});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (Exception e) { /* eat*/ }
}
return count;
}
@Override public void onProgressUpdate(Integer... vals) {
mTv.setText(vals[0]);
}
@Override public void onPostExecute(Integer inte) {
Toast.makeText(context, "We read " + inte + " lines from the url.", Toast.LENGTH_LONG).show();
}
}
答案 3 :(得分:1)
....loadQuery();
}
void loadQuery() {
new Thread(new Runnable() {
public void run() {
String qO = getIntent().getStringExtra("QUERY_ORDER");
String php = getIntent().getStringExtra("PHP_KEY");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://10.0.2.2/Andaero/php/" + php + qO + ".php");
Log.e("log_tag", "Fetched " + php + qO + ".php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
}
}).start();
}