试图修复NetworkOnMainThreadException但给吐司错误吐司、错误、NetworkOnMainThreadException

由网友(不眠敲钟人)分享简介:我试图修复一个线程错误我的$ P $之一pferred例子从Java code爱好者。I'm trying to fix a Thread error for one of my preferred examples from java code geeks.这里的code:public class JsonPa...

我试图修复一个线程错误我的$ P $之一pferred例子从Java code爱好者。

I'm trying to fix a Thread error for one of my preferred examples from java code geeks.

这里的code:

public class JsonParsingActivity extends Activity {

        String url = "http://search.twitter.com/search.json?q=javacodegeeks";

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            new RetreiveFeedTask().execute(url);

        }
    }

在RetrieveFeedTask:

The RetrieveFeedTask:

public class RetreiveFeedTask extends
        AsyncTask<String, Void, JsonParsingActivity> {

    private Exception exception;
    String url = "http://search.twitter.com/search.json?q=javacodegeeks";

    protected JsonParsingActivity doInBackground(String... urls) {
        try {

            InputStream source = retrieveStream(url);

            Gson gson = new Gson();

            Reader reader = new InputStreamReader(source);

            SearchResponse response = gson.fromJson(reader, SearchResponse.class);

            // Toast.makeText(this, response.query, Toast.LENGTH_SHORT).show();

            List<Result> results = response.results;

            for (Result result : results) {
                Toast.makeText(JsonParsingActivity.class, result.fromUser, Toast.LENGTH_SHORT).show();
            }

        } catch (Exception e) {
            this.exception = e;
            return null;
        }
    }

    protected void onPostExecute(JsonParsingActivity feed) {
        // TODO: check this.exception
        // TODO: do something with the feed
    }

    private InputStream retrieveStream(String url) {

        DefaultHttpClient client = new DefaultHttpClient();

        HttpGet getRequest = new HttpGet(url);

        try {

            HttpResponse getResponse = client.execute(getRequest);
            final int statusCode = getResponse.getStatusLine().getStatusCode();

            if (statusCode != HttpStatus.SC_OK) {
                Log.w(getClass().getSimpleName(), "Error " + statusCode
                        + " for URL " + url);
                return null;
            }

            HttpEntity getResponseEntity = getResponse.getEntity();
            return getResponseEntity.getContent();

        } catch (IOException e) {
            getRequest.abort();
            Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
        }

        return null;

    }}

怎样做,如果没有错误的最好方法是什么? 目前它给人敬酒错误(?)。

What's the best way to do that without errors? It's currently giving a Toast error (?).

推荐答案

您不能做 UI 的东西,在 doInBackground()。因此,你无法显示吐司有。您需要将这个以 onPostExecute()或其他地方。也许 onProgressUpdate()

You can't do UI stuff in doInBackground(). Hence, you can't display a Toast there. You need to move this to onPostExecute() or somewhere else. Possibly onProgressUpdate()

您可以致电 publishProgress(结果)并显示吐司 onProgressUpdate() 返回结果 onPostExecute()并显示它。您还可以发送数据回一个活动的选项方法

You could call publishProgress(results) and show the Toast in onProgressUpdate() or return results to onPostExecute() and display it there. You also have the option of sending the data back to an Activity method

AsyncTask的

阅读全文

相关推荐

最新文章