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

  • Home
  • SEARCH
  • 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 7559387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:37:22+00:00 2026-05-30T12:37:22+00:00

I take json object generate from a PHP script on my server and then

  • 0

I take json object generate from a PHP script on my server and then parse it into a listview using a lazy load for the images. The problem is that the json will load relatively fast or it will hang for second or two depending on the response on the server, which can be frustrating. When I first open the app the hang is espcially annoying because it will hang at a black screen until the object has been loaded and then when I update the list in app it has the same but at least the view has been loaded. Here is my code to get the json:

public void getJson(String selection, String url) {
    JSONObject json = null;
    String formatedcat = selection.toLowerCase();
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    json = JSONfunctions
            .getJSONfromURL(url);
    try {
        //the array title that you parse
        JSONArray category = json.getJSONArray(formatedcat);
        for (int i = 0; i < category.length(); i++) {               
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject c = category.getJSONObject(i);
            map.put("id", String.valueOf(i));
            map.put("name",
                    c.getString("title"));
            //map.put("text",c.getString("title"));
            map.put("ts",c.getString("run_date") );
            map.put("image","http:"+c.getString("url"));
            mylist.add(map);
        }
    } catch (JSONException e) {

    }
    ListAdapter adapter = new JsonAdapter(this, mylist, R.layout.list,
            new String[] { "name", "text", "ts"}, new int[] { R.id.item_title,
                    R.id.item_subtitle, R.id.timestamp});
    setListAdapter(adapter);        
}

Adapter Code:

public class JsonAdapter extends SimpleAdapter {

    public ImageManager imageManager;
    public ListActivity context;
    public ArrayList<HashMap<String,String>> list;
    public String[] fieldNames;
    public int[] fieldTargetIds;

    public JsonAdapter(ListActivity c, 
            ArrayList<HashMap<String, String>> mylist,
            int textViewResourceId,
            String[] fieldNames,
            int[] fieldTargetIds) {
        super(c, mylist, textViewResourceId, fieldNames, fieldTargetIds );
        this.context = c;
        this.list = mylist;
        this.fieldNames = fieldNames;
        this.fieldTargetIds = fieldTargetIds;
        this.imageManager = new ImageManager(context.getApplicationContext());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        if (row == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = vi.inflate(R.layout.list, null);
        }
        //super.getView(position, convertView, parent);
        ImageView imgView = (ImageView) row.findViewById(R.id.doodlepic);

        try {
            String url = list.get(position).get("image");
            imgView.setTag(url);
            imageManager.displayImage(url, context, imgView);
        } catch (Exception e) {

        }

        for (int i=0; i<fieldNames.length; i++) {
            TextView tv = (TextView) row.findViewById(fieldTargetIds[i]);
            tv.setText(list.get(position).get(fieldNames[i]));              
        }


        return row;
    }

}

How could I implement an async task to show a progress dialog while the json object is being generated and downloaded? I’ve tried using threads and Async task but I can’t quite figure out how to break apart my code into the appropriate parts.

  • 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-05-30T12:37:24+00:00Added an answer on May 30, 2026 at 12:37 pm

    The onPreExecute , onPostExecute of AsyncTask will run in the UI thread, the doInBackground will run in another thread, so below code should just fine for you

    public class YourActivity extends Activiy{
       public void getJson(String selection, String url) { 
               new LoadJsonTask().execute( selection, url);
    
       }
       private class LoadJsonTask extends AsyncTask<String, Void, ArrayList<HashMap<String, String>> > {
           ProgressDialog dialog ;
           protected void onPreExecute (){
                dialog = ProgressDialog.show(YourActivity.this ,"title","message");
    
           }
           protected ArrayList<HashMap<String, String>> doInBackground (String... params){
               return doGetJson(params[0],params[1]);
           }
           protected void onPostExecute(ArrayList<HashMap<String, String>> mylist){
    
                ListAdapter adapter = new JsonAdapter(YourActivity.this, mylist, R.layout.list,
                  new String[] { "name", "text", "ts"}, new int[] { R.id.item_title,
                    R.id.item_subtitle, R.id.timestamp});
                setListAdapter(adapter);
                dialog.dismiss();
           }
        }
    
     public ArrayList<HashMap<String, String>> doGetJson(String selection, String url) {
         JSONObject json = null;
         String formatedcat = selection.toLowerCase();
         ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
         json = JSONfunctions
            .getJSONfromURL(url);
         try {
        //the array title that you parse
        JSONArray category = json.getJSONArray(formatedcat);
        for (int i = 0; i < category.length(); i++) {               
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject c = category.getJSONObject(i);
            map.put("id", String.valueOf(i));
            map.put("name",
                    c.getString("title"));
            //map.put("text",c.getString("title"));
            map.put("ts",c.getString("run_date") );
            map.put("image","http:"+c.getString("url"));
            mylist.add(map);
        }
    } catch (JSONException e) {
    
    }
       return mylist;
      ....   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the jQuery code below, I'm trying to take a JSON object to create
I'm using GraniteDS Actionscript code generation templates that let's me take a Java object
I need to convert the output of an .aspx page into a JSON object
So I'm getting an object back from a server that looks like this: {
I am attempting to parse the incoming JSON from XBMC (Eden Beta v3), an
I've generated some JSON and I'm trying to pull it into an object in
I'm trying to take an existing JSON object and create smaller JSON chunks out
Im using Restkit OM2 to take in a json and map to objects on
I am learning how to parse and generate JSON with the JSON gem. I
I can take json object like this : {title:this i the title, description:this is

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.