【问题标题】:networkOnMainThread Exception AndroidnetworkOnMainThread 异常 Android
【发布时间】:2012-03-15 23:38:24
【问题描述】:

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

//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,但我的应用程序只是强制关闭。

有人知道怎么做吗?

【问题讨论】:

    标签: android networking android-asynctask android-4.0-ice-cream-sandwich http-get


    【解决方案1】:

    但显然在 Ice Cream Sandwich 中你不能在主线程中进行网络操作

    你可以,但它会在 LogCat 中给你一个警告。而且您不应该在主应用程序线程上执行网络操作,因为它会冻结您的 UI 并可能给您一个“应用程序无响应”(ANR) 对话框。

    但是 AsyncTask 类让我感到困惑并且无法正常工作

    在您当前的代码中,您实际上并未执行HttpGet 请求。

    它不喜欢我的 HttpGet request = new HttpGet(uri) 它说要将 uri 更改为 URI,但它已经是!

    不,不是。

    URI 后面的 ... 在您的 doInBackground() 声明中表示此方法接受可变数量的参数。您的uri 参数实际上是URI[]。如果你只用一个URI 调用execute(),你会通过uri[0] 得到URI,而不是uri

    【讨论】:

    • 糟糕,我忘了添加:'try { response = client.execute(request); } catch (ClientProtocolException e) {' 但它仍然强制关闭。我什至需要类的任何参数吗?我可以在 'doInBackground()' 方法中创建 URI 吗?
    【解决方案2】:

    试试这个例子:

        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文件,行为正确吗?

    【讨论】:

    • 是的!太感谢了!我认为不起作用的是 Toast 通知。我必须返回我的字符串,然后解码 JSON。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    相关资源
    最近更新 更多