0

I am using this code to load data from online database to my android application . I am wondering what can i add to make this code better ? Sometimes the progress dialog keeps spinning and never gets the data, the application is stuck then, any ideas on how i can prevent that ?

    class LoadAllSections extends AsyncTask<String, String, String>
{

    // make a progress dialog appear with the selected specifics
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Loading all sections, please wait");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    // in the background run this code to retrieve data from the server
    protected String doInBackground(String... args) 
    {
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        JSONObject json = jParser.makeHttpRequest(url_Sections,"POST", params);
        try 
        {
            int success = json.getInt(TAG_SUCCESS);
            sections = json.getJSONArray(TAG_SECTIONS);

            if (success == 1) 
            {
                for (int i = 0; i < sections.length(); i++) 
                {
                    JSONObject c = sections.getJSONObject(i);

                    section_id = c.getString(TAG_SECTION_ID);
                    section_name = c.getString(TAG_SECTION_NAME);
                    section_desc = c.getString(TAG_SECTION_DESC);
                    section_image = c.getString(TAG_SECTION_IMAGE);
                    section_valid = c.getString(TAG_SECTION_VALID);     

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

                    sectionmap.put(TAG_SECTION_ID, section_id);
                    sectionmap.put(TAG_SECTION_NAME, section_name);
                    sectionmap.put(TAG_SECTION_DESC, section_desc);
                    sectionmap.put(TAG_SECTION_IMAGE, section_image);
                    sectionmap.put(TAG_SECTION_VALID, section_valid);

                    sectionlist.add(sectionmap);
                }
            }
            else
            {
            finish();
            }
        }
        catch (JSONException e) 
        {
            e.printStackTrace();
        }
        return null;
    }

    // disable the progress dialog and load data to the gridview
    protected void onPostExecute(String file_url) 
    {
        pDialog.dismiss();
        adapter=new SectionAdapter(MainActivity.this,sectionlist);
        SectionsGridView.setAdapter(adapter);
    }
}
Jasser
  • 65
  • 1
  • 9
  • No way will this code work... You are asking about "loading data from online MySQL database to android application" and your sending a "POST"?? I will see how this should work anyhow?? – Martin Pfeffer Oct 10 '14 at 22:43

1 Answers1

0

I wanted to add a comment, but I am not allowed to. Don't have enough reputation :-(

  • Pass url_section as argument to doInBackground instead of making it global.
  • I would place the httpRequest insde a try catch block.
  • Did you set the timeout, if the httpRequest is not answering? I would set that to 60 seconds. I think by default this is set to 600 seconds.
  • Why do you pass the file_url to onPostExecute instead of passing the sectionList?
  • Take a look at AsyncTask. If you don't want to pass anything between the methods, you can also use Void. So in your case AsyncTask would also do it.
styssi
  • 200
  • 2
  • 10
  • thanks ... i will check all the above suggestions and will get back to u ... i am relatively new to java android, so i may need some help coding what u suggested . – Jasser Apr 17 '14 at 13:59
  • I placed the httprequest inside try catch block .... how do i set the timeout ? any directions ? – Jasser Apr 17 '14 at 14:09
  • and i can call the entire class and it will execute the onPreExecute and doInBackground and onPostExecute .... how can i control what to pass to each ? i thought i could only call the class LoadAllSections ? – Jasser Apr 17 '14 at 14:11
  • Adding the httpRequest inside try/catch doesn't fix your problem, but it makes your app more stable. It won't crash, if there are any issues with the request. Checkout this link http://stackoverflow.com/questions/15867930/how-to-set-connection-timeout-for-jsonparser-makehttprequest-in-android for setting the timeout The Google API describes pretty good how to use AsyncTasks. http://developer.android.com/reference/android/os/AsyncTask.html – styssi Apr 17 '14 at 21:11