android – 如何添加线程?

这是从this post’s Question继续.

我无法想象如何从主UI线程添加单独的线程来执行从服务器收集数据的工作.我之前从未做过线程,我认为我构造的类中的这个实例比我能找到的任何一个例子更先进.

我的课程的任何帮助和发布的修订将不胜感激.

谢谢!

@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));
    }

请参阅下面的答案

最佳答案
你应该简单地使用AsyncTask

这是一个关于如何使用itTutorial的好教程

以下是下载网页并将结果返回主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);
    }
}

转载注明原文:android – 如何添加线程? - 代码日志