异步任务失败

时间:2013-07-06 16:29:31

标签: android android-asynctask lag

在我的活动中,我设计了一个导航抽屉,所以当我点击片段时,我的活动会像这样挂起:

myactivity http://imageshack.com/scaled/large/844/z5ta.png

冻结5~6秒,所以我想显示一个旋转轮。 我知道我应该扩展AsyncTask但它不起作用! 这是我试过的:

private void samsung{
    // TODO Auto-generated method stub
    Url = "/// ////////////// //////////// /////////////.php";
    new loadSomeStuff().execute(Url);
}

这是我的aSyncTask类:

public class loadSomeStuff extends AsyncTask<String, Integer, String> {
    ProgressDialog dialog;

    protected void onPreExecute(String f) {
        dialog = new ProgressDialog(Fragment9.this.getSherlockActivity());
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setCancelable(false);
        dialog.setIndeterminate(true);

        dialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        beanClass.clear();
        list.setAdapter(null);
        String result = "";
        InputStream isr = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpost = new HttpPost(Url);
            HttpResponse resposne = httpclient.execute(httpost);
            HttpEntity entity = resposne.getEntity();
            isr = entity.getContent();
        } catch (Exception e) {
            Log.e("log_httpconnection",
                    "error in http connection" + e.toString());
            Toast.makeText(getSherlockActivity(), "No Connection",
                    Toast.LENGTH_SHORT).show();
        }

        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(isr, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            isr.close();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_json", "Error converting Result " + e.toString());
        }
        try {
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json = jArray.getJSONObject(i);
                beanClass.add(new BeanClass(json.getString("PhoneName"),
                        json.getString("ModelNumber"), json
                                .getString("PhonePrice"), json
                                .getString("imageurl")));

            }
            dbHelpersamsung.deletesamsunglebanonphones();
            dbHelpersamsung.insertthephonessamsunglebanon();
        } catch (Exception e) {
            Log.e("lag_tag", "ERROR PARSING DATA" + e.toString());
            displaylistviewsamsunglebanon();
        }
        return null;
    }

    protected void onProgressUpdate(Integer... progress) {

    }

    protected void onPostExecute(String result) {
        list.setAdapter(new MyListAdapter(getActivity(), beanClass));

    }
}

1 个答案:

答案 0 :(得分:1)

你有这个

  list.setAdapter(null);

这个

     Toast.makeText(getSherlockActivity(), "No Connection",
                Toast.LENGTH_SHORT).show();

无法从doInbackground更新ui。更新onPostExecuterunOnUiThread

中的ui