Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9203149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:24:27+00:00 2026-06-17T23:24:27+00:00

I am fetching JSON from URL using AsycTask, parsing it and displaying the data

  • 0

I am fetching JSON from URL using AsycTask, parsing it and displaying the data onto the listView. I also click the refresh button to once again fetch the JSON from URL for some updating. When i click the refresh button the following takes place;
1) adapter.clear() : clearing the listView’s adapter to populate the new data.
2) Fetch JSON from url, parse and display the newly fetched data onto the listView.
3) Meanwhile when fetching the data, I show a progress dialog.

But when i click on the refresh button, the main UI thread freezes during the execution of AsynTask. I mean the refresh button looks pressed until the AsynTask finishes the job as shown below.
enter image description here

Here is my code:

public class AndroidJSONParsingActivity extends SherlockActivity {

    // JSON Node names
    ArrayList<HashMap<String, String>> placeList = new ArrayList<HashMap<String, String>>();
    private static final String TAG_PLACES = "places";
    private static final String TAG_PLACE_NAME = "name";
    private static final String TAG_PLACE_CATEGORY = "category";
    private static final String TAG_PLACE_DISTANCE = "distance";
    private static final String TAG_TRAVEL_TIME = "travel_time";
    ListAdapter adapter;
    public static Context con;
    JSONArray places = null;
    ListView list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        con = this;
        list = (ListView) findViewById(R.id.listView1);
        parseJSON();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.activity_main, menu);

        MenuItem menu_item1 = menu.findItem(R.id.menu_item1);

        menu_item1.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(getApplicationContext(), "refresh",
                        Toast.LENGTH_LONG).show();
                placeList.clear();
                parseJSON();
                return false;
            }
        });

        return true;

    }

    public void parseJSON() {
        try {
            placeList = new JSONParsingTask().execute().get();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        adapter = new SimpleAdapter(this, placeList, R.layout.list_item,
                new String[] { TAG_PLACE_NAME, TAG_PLACE_CATEGORY,
                        TAG_PLACE_DISTANCE, TAG_TRAVEL_TIME }, new int[] {
                        R.id.name, R.id.category, R.id.distance,
                        R.id.travel_time });

        list.setAdapter(adapter);

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String name = ((TextView) view.findViewById(R.id.name))
                        .getText().toString();
                String category = ((TextView) view.findViewById(R.id.category))
                        .getText().toString();
                String distance = ((TextView) view.findViewById(R.id.distance))
                        .getText().toString();
                String travel_time = ((TextView) view
                        .findViewById(R.id.travel_time)).getText().toString();

                Intent in = new Intent(getApplicationContext(),
                        SingleMenuItemActivity.class);
                in.putExtra(TAG_PLACE_NAME, name);
                in.putExtra(TAG_PLACE_CATEGORY, category);
                in.putExtra(TAG_PLACE_DISTANCE, distance);
                in.putExtra(TAG_TRAVEL_TIME, travel_time);
                startActivity(in);

            }
        });

    }

}

class JSONParsingTask extends
        AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {
    public Boolean flag = false;
    private static String url = "http://some - url";

    // JSON Node names

    private static final String TAG_PLACES = "places";
    private static final String TAG_PLACE_NAME = "name";
    private static final String TAG_PLACE_CATEGORY = "category";
    private static final String TAG_PLACE_DISTANCE = "distance";
    private static final String TAG_TRAVEL_TIME = "travel_time";
    ListAdapter adapter;
    JSONParser jParser;
    ListView list;
    static ArrayList<HashMap<String, String>> placeList = new ArrayList<HashMap<String, String>>();
    JSONArray places = null;

    ProgressDialog JSONParsingDialog = new ProgressDialog(
            AndroidJSONParsingActivity.con);

    @Override
    protected void onPreExecute() {
        JSONParsingDialog.setMessage("Preparing data...");
        JSONParsingDialog.setCanceledOnTouchOutside(false);
        JSONParsingDialog.setCancelable(false);
        JSONParsingDialog.show();
    }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(Void... params) {
        JSONParser jParser = new JSONParser();

        final JSONObject json = jParser.getJSONFromUrl(url);

        try {
            places = json.getJSONArray(TAG_PLACES);

            for (int i = 0; i < places.length(); i++) {
                JSONObject c = places.getJSONObject(i);

                String name = c.getString(TAG_PLACE_NAME);
                String category = c.getString(TAG_PLACE_CATEGORY);
                String distance = c.getString(TAG_PLACE_DISTANCE);
                String travel_time = c.getString(TAG_TRAVEL_TIME);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_PLACE_NAME, name);
                map.put(TAG_PLACE_CATEGORY, category);
                map.put(TAG_PLACE_DISTANCE, distance);
                map.put(TAG_TRAVEL_TIME, travel_time);

                placeList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return placeList;
    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        JSONParsingDialog.dismiss();
    }

}

All i need is, when i click on the refresh button, it should be smooth and should not freeze until the AsynTask is completed. Any idea how can i do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:24:29+00:00Added an answer on June 17, 2026 at 11:24 pm

    use

    new JSONParsingTask().execute(); 
    

    .get() blocks the caller thread.

    more info: Looking for good example of using get() with an AsyncTask in android

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fetching data from a URL using an AJAX call. It is giving
Possible Duplicate: Sending and Parsing JSON in Android I am fetching JSON Data from
I just fetched some data from JSon and listed into my Listview. It's fetching
I am fetching some data from a site using its API-key and some other
I'm fetching some data from an MSSQL table using the mssql_fetch_object, but the text
I'm using ASIHTTPRequest to pull the JSON data from WCF. I then looping through
I am fetching the data in JSON format from YouTube but it returns the
I'm fetching JSON from my ASP .NET website using the following jQuery: $.ajax({ type:
In one of my applications, i am fetching json data from php and rendering
I'm fetching a row of data from a mysql-server using php, then encode it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.