简体   繁体   中英

adding Progress Bar to my dataCall function - Android

How do I add a Progress Bar when a user selects a spinner item that triggers a http request. Do I add it to the class doing the http request (dataCall) or the main activity that has the spinner in it?

I have a class DataCall that when called does a http call to a php script that gets data from a MySQL database and returns data in a JSON format. This class is called after a spinner item is selected. The data returned is added to another spinner in the same activity. I am trying to show the progress wheel when the class DataCall is doing its thing. Below is my code for calling DataCall from my MainActivity and the code from DataCall .

MainActivity that calls DataCall (See below) when the spinner is selected. When DataCall returns data it updates another spinner with the new data.

statespinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
     public void onItemSelected(AdapterView<?> parent, View view, int position,long arg3) 
     {
      int id = parent.getId();

      if (spinner1_count2 < spinner1_count1 ) {
          spinner1_count2++;  } 
         else 
         {
             city.clear();
             String item = statespinner.getSelectedItem().toString();
             String spinnerContentType = "city";
             String spinnerURL = "getStoreCity.php?state=" + item;


//CALLING DATACALL BELOW
                 String city_data =  DataCall.getJSON(spinnerURL,spinnerContentType);
             Log.d(TAG, city_data);
             String state_spinner_log = "STATE SPINNER";
             Log.d(TAG, state_spinner_log);

             JSONArray jsonArray;    
             try {

                 cityjsonArray = new JSONArray(city_data);


                 for (int i=0; i<cityjsonArray.length(); i++)
                 {   
                     String styleValue2 = cityjsonArray.getJSONArray(i).getString(0);    
                     Log.d(TAG, styleValue2);
                     city.add(styleValue2);
                     adapter2.notifyDataSetChanged();

                 }
                //

                 } catch (JSONException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }




          }

     }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }

    });

DataCall Class:

 public class DataCall extends Activity {
        private static final String TAG = "MyApp";


         public static String getJSON(String myUrlString, String contentType){  

                    String line = null;
                    String tag_value = null;
                    try {

                        DefaultHttpClient httpClient = new DefaultHttpClient();

                        HttpGet httpPost = new HttpGet("http://www.mywebsite.com/getdata/" + myUrlString);

                        HttpResponse httpResponse = httpClient.execute(httpPost);
                        HttpEntity httpEntity = httpResponse.getEntity();
                        line = EntityUtils.toString(httpEntity);

                    } catch (UnsupportedEncodingException e) {
                        line = "<results status=\"error\"><msg>Can't connect to server1</msg></results>";
                    } catch (MalformedURLException e) {
                        line = "<results status=\"error\"><msg>Can't connect to server2</msg></results>";
                    } catch (IOException e) {
                        line = "<results status=\"error\"><msg>Can't connect to server3</msg></results>";
                    }catch (Exception anything) {
                        //Whatever

                    }

                    return line;

            }


    }

I think you're going to have to move this web call out to a different thread as it will take an unknown amount of time to run and may cause an ANR.

If you use ASyncTask it has an inbuilt mechanism for updating a progress bar as the background thread runs. There is a lot of valuable information at this link

http://www.vogella.de/articles/AndroidPerformance/article.html

Hope this helps, m

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM