本文介绍了networkOnMainThread 异常 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问服务器,以便接收 JSON 字符串.但显然在 Ice Cream Sandwich 中你不能在主线程中进行网络操作,但是 AsyncTask 类让我感到困惑并且无法工作.这是我到目前为止所拥有的:

I'm trying to access a server so I can receive a JSON String. But apparently in Ice Cream Sandwich you can't do network operations in the main thread, but the AsyncTask class is confusing me and not working. Here's what I have so far:

//up in main code
Void blah = null;
URI uri = "kubie.dyndns-home.com/R2Bar2/ingredients.php";
new DownloadFilesTask().execute(uri , blah, blah);


private class DownloadFilesTask extends AsyncTask<URI, Void, Void> {
        protected Void doInBackground(URI... uri) {
            HttpClient client = new DefaultHttpClient();
            String json = "";
            int duration = Toast.LENGTH_SHORT;
            try {
                HttpResponse response = null;
                BufferedReader rd = null;
                String line = "";
                HttpGet request = new HttpGet(uri);
            } catch (URISyntaxException e1) {

            }
return null;
        }

        protected void onProgressUpdate(Integer... progress) {

        }

        protected void onPostExecute(Long result) {

        }

它不喜欢我的 HttpGet request = new HttpGet(uri)它说要将 uri 更改为 URI,但它已经是了!我已经尝试将所有参数更改为 Void,但我的应用程序只是强制关闭.

It's not liking my HttpGet request = new HttpGet(uri)It says to change uri to URI, but it already is!I've tried changing all parameters to Void, but my app just force closes.

有人知道怎么做吗?

推荐答案

试试这个例子:

    public class Test_anroidActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String uri = new String("http://kubie.dyndns-home.com/R2Bar2/ingredients.php");
        new DownloadFilesTask().execute(uri , null, null);
    }
    private class DownloadFilesTask extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... urls) {
            HttpClient client = new DefaultHttpClient();
            String json = "";
            try {
                String line = "";
                HttpGet request = new HttpGet(urls[0]);
                HttpResponse response = client.execute(request);
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                while ((line = rd.readLine()) != null) {
                    json += line + System.getProperty("line.separator");
                }
            } catch (IllegalArgumentException e1) {
                e1.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
            return json;
        }

        protected void onProgressUpdate(Void... progress) {

        }

        protected void onPostExecute(String result) {

        }
    }
}

最后一个,你的服务器返回 php 文件,这是正确的行为吗?

The last one, your server returns php file, is it right behaviour?

这篇关于networkOnMainThread 异常 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:30