user1654206
user1654206

Reputation:

Async Task Failure

In my activity i have designed a navigation drawer, so when i click on the fragment my activity hangs up like this :

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

It freezes for 5~6 seconds, so i want to display a spinning wheel. I know i should extend AsyncTask but it didn't work! Here's what i tried :

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

And Here's my aSyncTask Class :

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

    }
}

Upvotes: 0

Views: 309

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

You have this

  list.setAdapter(null);

And this

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

Updating ui from doInbackground is not possible. Update ui in onPostExecute or you runOnUiThread

Upvotes: 1

Related Questions