networkOnMainThread Exception Android

2019-01-20 16:27发布

问题:

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) {

        }

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.

Anyone know how to do this?

回答1:

Try this example:

    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) {

        }
    }
}

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



回答2:

But apparently in Ice Cream Sandwich you can't do network operations in the main thread

You can, but it will give you a warning in LogCat. And you should not do network operations on the main application thread, as it will freeze your UI and possibly give you an "Application Not Responding" (ANR) dialog.

but the AsyncTask class is confusing me and not working

In your current code, you are not actually executing the HttpGet request.

It's not liking my HttpGet request = new HttpGet(uri) It says to change uri to URI, but it already is!

No, it is not.

The ... after URI in your doInBackground() declaration means that this method accepts a variable number of arguments. Your uri parameter is, in effect, a URI[]. If you are only ever calling execute() with one URI, you get at that URI via uri[0], not uri.