title: How to access json array that is passed from restful web service in android?
body: <p>I have successfully created a rest web service and it returns jsonarray which has two fields 
  id and city from data base.</p>
  
  <pre><code>My resr web service is 
  </code></pre>
  
  <p>@GET</p>
  
  <pre><code>@Path("city")
  
  @Produces("application/json")
  
  public String getJson() {
  
      PropertyPojo propojo=null;
  
      ArrayList cityList = new ArrayList();
  
      JSONArray list = new JSONArray();
  
       Map m1 = new LinkedHashMap();
  
       List  l1 = new LinkedList();
  
       String jsonString = null;
  
      try{
          cityList=PDao.CityList();
           Iterator it=cityList.iterator();
  
                  while(it.hasNext())
                   {
                      propojo=(PropertyPojo)it.next();
  
                      m1.put(propojo.getKeyid(),propojo.getKeyvalue());
  
                   }
  
  
      }catch(Exception e){
  
      }
      l1.add(m1);
      jsonString = JSONValue.toJSONString(l1);
      return jsonString;
  }
  </code></pre>
  
  <p>I just need to put these values into a spinner...</p>
  
  <p><strong>My android code is</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
  
      Spinner spinner;
  
      private static final String SERVICE_URL = "http://192.168.1.6:8080/eSava_RestWeb/webresources/service";
  
      private static final String TAG = "AndroidRESTClientActivity";
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          spinner = (Spinner) findViewById(R.id.city);
      }
  
      public void retrieveSampleData(View vw) {
  
          String sampleURL = SERVICE_URL + "/city";
  
          WebServiceTask wst = new WebServiceTask(WebServiceTask.GET_TASK,
                  this, "GETting data...");
  
          wst.execute(new String[] { sampleURL });
  
      }
  
      @SuppressLint("NewApi")
      public void handleResponse(String response) {
  
          try {
              // JSONObject jso = new JSONObject(response);
              JSONArray json = new JSONArray(response);
  
              ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
              JSONArray jsonArray = (JSONArray) json;
              if (jsonArray != null) {
                  int len = jsonArray.length();
                  for (int i = 0; i &lt; len; i++) {
                      list.add(jsonArray.get(i).toString());
                  }
              }
  
              Spinner s = (Spinner) findViewById(R.id.city);
              ArrayAdapter adapter = new ArrayAdapter(this,
                      android.R.layout.simple_spinner_item, list);
              s.setAdapter(adapter);
  
          } catch (Exception e) {
              Log.e(TAG, e.getLocalizedMessage(), e);
          }
  
      }
  
      private class WebServiceTask extends AsyncTask&lt;String, Integer, String&gt; {
  
          public static final int POST_TASK = 1;
          public static final int GET_TASK = 2;
  
          private static final String TAG = "WebServiceTask";
  
          // connection timeout, in milliseconds (waiting to connect)
          private static final int CONN_TIMEOUT = 3000;
  
          // socket timeout, in milliseconds (waiting for data)
          private static final int SOCKET_TIMEOUT = 5000;
  
          private int taskType = GET_TASK;
          private Context mContext = null;
          private String processMessage = "Processing...";
  
          private ArrayList&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
  
          private ProgressDialog pDlg = null;
  
          public WebServiceTask(int taskType, Context mContext,
                  String processMessage) {
  
              this.taskType = taskType;
              this.mContext = mContext;
              this.processMessage = processMessage;
          }
  
          public void addNameValuePair(String name, String value) {
  
              params.add(new BasicNameValuePair(name, value));
          }
  
          private void showProgressDialog() {
  
              pDlg = new ProgressDialog(mContext);
              pDlg.setMessage(processMessage);
              pDlg.setProgressDrawable(mContext.getWallpaper());
              pDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
              pDlg.setCancelable(false);
              pDlg.show();
  
          }
  
          @Override
          protected void onPreExecute() {
  
              showProgressDialog();
  
          }
  
          protected String doInBackground(String... urls) {
  
              String url = urls[0];
              String result = "";
  
              HttpResponse response = doResponse(url);
  
              if (response == null) {
                  return result;
              } else {
  
                  try {
  
                      result = inputStreamToString(response.getEntity()
                              .getContent());
  
                  } catch (IllegalStateException e) {
                      Log.e(TAG, e.getLocalizedMessage(), e);
  
                  } catch (IOException e) {
                      Log.e(TAG, e.getLocalizedMessage(), e);
                  }
  
              }
  
              return result;
          }
  
          @Override
          protected void onPostExecute(String response) {
  
              JSONArray jsArray;
              // jsArray = new JSONArray(response);
              handleResponse(response);
              pDlg.dismiss();
  
          }
  
          // Establish connection and socket (data retrieval) timeouts
          private HttpParams getHttpParams() {
  
              HttpParams htpp = new BasicHttpParams();
  
              HttpConnectionParams.setConnectionTimeout(htpp, CONN_TIMEOUT);
              HttpConnectionParams.setSoTimeout(htpp, SOCKET_TIMEOUT);
  
              return htpp;
          }
  
          private HttpResponse doResponse(String url) {
  
              // Use our connection and data timeouts as parameters for our
              // DefaultHttpClient
              HttpClient httpclient = new DefaultHttpClient(getHttpParams());
  
              HttpResponse response = null;
  
              try {
                  switch (taskType) {
  
                  case POST_TASK:
                      HttpPost httppost = new HttpPost(url);
                      // Add parameters
                      httppost.setEntity(new UrlEncodedFormEntity(params));
  
                      response = httpclient.execute(httppost);
                      break;
                  case GET_TASK:
                      HttpGet httpget = new HttpGet(url);
                      response = httpclient.execute(httpget);
                      break;
                  }
              } catch (Exception e) {
  
                  Log.e(TAG, e.getLocalizedMessage(), e);
  
              }
  
              return response;
          }
  
          private String inputStreamToString(InputStream is) {
  
              String line = "";
              StringBuilder total = new StringBuilder();
  
              // Wrap a BufferedReader around the InputStream
              BufferedReader rd = new BufferedReader(
                      new InputStreamReader(is));
  
              try {
                  // Read response until the end
                  while ((line = rd.readLine()) != null) {
                      total.append(line);
                  }
              } catch (IOException e) {
                  Log.e(TAG, e.getLocalizedMessage(), e);
              }
  
              // Return full string
              return total.toString();
          }
  
      }
  }
  </code></pre>