title: how to set content in spinner dynamicly using async task?
body: <p>I am new in android. I am trying to fetch data in spinner from database. But I don't know why my list is not set in spinner.</p>
  
  <p><strong>My code is</strong>  </p>
  
  <pre><code>public class AddBasicDetail extends Fragment implements AdapterView.OnItemSelectedListener {
  
      // array list for spinner adapter
      private ArrayList&lt;State&gt; categoriesList;
      ProgressDialog pDialog;
  
      Spinner sp_state;
      // API urls
      // Url to create new category
      private String URL_NEW_CATEGORY = "http://mandirdekhoo.com/app/md_eng/state.php";
  
      public AddBasicDetail() {
          // Required empty public constructor
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          View v = inflater.inflate(R.layout.add_basic_detail, container, false);
  
          sp_state = (Spinner) v.findViewById(R.id.sp_state);
          categoriesList = new ArrayList&lt;State&gt;();
  
          // spinner item select listener
          //sp_state.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) getActivity());
  
          new GetCategories().execute();
  
          return v;
      }
  
      /**
      * Adding spinner data
      * */
      private void populateSpinner() {
           List&lt;String&gt; lables = new ArrayList&lt;String&gt;();
  
           //txtCategory.setText("");
  
           for (int i = 0; i &lt; categoriesList.size(); i++) {
                lables.add(categoriesList.get(i).getName());
           }
  
           // Creating adapter for spinner
           ArrayAdapter&lt;String&gt; spinnerAdapter = new ArrayAdapter&lt;String&gt;(getContext(),
                          android.R.layout.simple_spinner_item, lables);
  
           // Drop down layout style - list view with radio button
           spinnerAdapter                           .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
           // attaching data adapter to spinner
           sp_state.setAdapter(spinnerAdapter);
      }
  
      /**
      * Async task to get all food categories
      * */
      private class GetCategories extends AsyncTask&lt;Void, Void, Void&gt; {
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              pDialog = new ProgressDialog(getContext());
              pDialog.setMessage("Fetching food categories..");
              pDialog.setCancelable(false);
              pDialog.show();
  
          }
  
          @Override
          protected Void doInBackground(Void... arg0) {
              ServiceHandler jsonParser = new ServiceHandler();
              String json = jsonParser.makeServiceCall(URL_NEW_CATEGORY, ServiceHandler.GET);
  
              Log.e("Response: ", "&gt; " + json);
  
              if (json != null) {
  
                  try {
                      JSONObject jsonObj = new JSONObject(json);
                      Log.e("my response: ", "&gt; " + jsonObj);
                      if (jsonObj != null) {
                          JSONArray categories = jsonObj                                            .getJSONArray("result");
  
                                  for (int i = 0; i &lt; categories.length(); i++) {
                                      JSONObject catObj = (JSONObject) categories.get(i);
                                      State cat = new State(catObj.getInt("id"),
                                              catObj.getString("state_name"));
                                      categoriesList.add(cat);
                                  }
                              }
  
                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
  
                      } else {
                          Log.e("JSON Data", "Didn't receive any data from server!");
                      }
  
                      return null;
                  }
  
                  @Override
                  protected void onPostExecute(Void result) {
                      super.onPostExecute(result);
                      if (pDialog.isShowing())
                          pDialog.dismiss();
                      //populateSpinner();
                  }
  
              }
              @Override
              public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                      Toast.makeText(
                          getContext(),
                          parent.getItemAtPosition(position).toString() + " Selected" ,
                          Toast.LENGTH_LONG).show();
  
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
              }
          }
  </code></pre>
  
  <p><strong>my service handler class is</strong> </p>
  
  <pre><code>public class ServiceHandler {
  
      static InputStream is = null;
      static String response = null;
      public final static int GET = 1;
      public final static int POST = 2;
  
      public ServiceHandler() {
  
      }
  
      /**
       * Making service call
       * @url - url to make request
       * @method - http request method
       * */
      public String makeServiceCall(String url, int method) {
          return this.makeServiceCall(url, method, null);
      }
  
      /**
       * Making service call
       * @url - url to make request
       * @method - http request method
       * @params - http request params
       * */
      public String makeServiceCall(String url, int method,
                                    List&lt;NameValuePair&gt; params) {
          try {
              // http client
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpEntity httpEntity = null;
              HttpResponse httpResponse = null;
  
              // Checking http request method type
              if (method == POST) {
                  HttpPost httpPost = new HttpPost(url);
                  // adding post params
                  if (params != null) {
                      httpPost.setEntity(new UrlEncodedFormEntity(params));
                  }
  
                  httpResponse = httpClient.execute(httpPost);
  
              } else if (method == GET) {
                  // appending params to url
                  if (params != null) {
                      String paramString = URLEncodedUtils
                              .format(params, "utf-8");
                      url += "?" + paramString;
                  }
                  HttpGet httpGet = new HttpGet(url);
  
                  httpResponse = httpClient.execute(httpGet);
  
              }
              httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();
  
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          try {
              BufferedReader reader = new BufferedReader(new InputStreamReader(
                      is, "UTF-8"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              is.close();
              response = sb.toString();
          } catch (Exception e) {
              Log.e("Buffer Error", "Error: " + e.toString());
          }
  
          return response;
  
      }
  
  }
  </code></pre>
  
  <p><strong>and</strong></p>
  
  <pre><code>public class State {
  
      private int id;
      private String name;
  
      public State(){}
  
      public State(int id, String name){
          this.id = id;
          this.name = name;
      }
  
      public void setId(int id){
          this.id = id;
      }
  
      public void setName(String name){
          this.name = name;
      }
  
      public int getId(){
          return this.id;
      }
  
      public String getName(){
          return this.name;
      }
  }
  </code></pre>
  
  <p>Please help</p>