for a Map (HashMap), you may use JSONObject.keys() or JSONObject.names() for retrieving all keys eg: a, b, c..., and then JSONObject.getInt(String name) get each of their mapped values. for a List of values, just try JSONObject.getInt(int index), JSONObject.length() to get values in a loop in you case. and create an ArrayAdapter for your spinner. wenbo
On Mar 5, 1:17 pm, Atif Musaddaq <[email protected]> wrote: > Hi, All > > I would like to generate a List view in android (spinner) from the list i > will get from PHP server. > > right now my server code is like this. > > <?php > $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); > > echo json_encode($arr); > > ?> > > my android code is this. > > public class MainActivity extends Activity { > private Button btn; > private TextView txt; > //Change this IP:PORT with you current IP:PORT > private static String SERVICE_URL = "myurl"; > > private String getTheTimeFromJSONString(String json_string) > { > try { > JSONObject j = new JSONObject(json_string); > return j.getString("current_date");} catch (JSONException e) { > > e.printStackTrace(); > return json_string;} > } > > private String execHttpRequest(String url) { > try { > HttpClient httpclient = new DefaultHttpClient(); > HttpGet httpget = new HttpGet(url); > //We want the JSON version of resource > httpget.addHeader("accept", "application/json"); > HttpResponse response = httpclient.execute(httpget); > HttpEntity entity = response.getEntity(); > return EntityUtils.toString(entity);} catch (Exception e) { > > e.printStackTrace(); > return e.getMessage(); > > } > } > > /** Called when the activity is first created. */ > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > btn = (Button) findViewById(R.id.btn_click_me); > txt = (TextView) findViewById(R.id.txt); > btn.setOnClickListener(new View.OnClickListener() { > public void onClick(View arg0) { > String s = execHttpRequest(SERVICE_URL); > txt.setText(getTheTimeFromJSONString(s)); > > } > }); > } > } > > I would like to convert this response in java array or ArrayList so i can > use it to populate Spinner. May i know how i can do this. Right now i am > getting the response as seen in the output image attached with this message. > > Please help me in this regard. > > -- > Musaddaq > > output.jpg > 104KViewDownload -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

