This one is working, enjoy.............. I will post l8r on code how
to generate a spinner from server json response.


package it.danieleteti.whitepaper01;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;

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 = "your url";

        private String getTheTimeFromJSONString(String json_string)
        {
                try {
                        Log.i("Output",json_string);
                        JSONArray jsonArray = new JSONArray(json_string);

                        List<String> list = new ArrayList<String>();
                        for (int i=0; i<jsonArray.length(); i++) {
                            list.add( jsonArray.getString(i) );
                            Log.i("Output",jsonArray.getString(i));

                        }
                        return "Nothing";
                } 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));
                        }
                });
        }
}

On Mar 5, 2:23 pm, Atif Musaddaq <[email protected]> wrote:
> i found this resourcehttp://www.embarcadero.com/rad-in-action/php-android
>
> but could not found the solution Yet.
>
> Atif
>
> On Sat, Mar 5, 2011 at 2: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
>
> --
> Atif Musaddaq
> Master in Media Informatics,
> RWTH Aachen & University of Bonn,
> Germany.
> [email protected]
>
> *”Design can be art. Design can be aesthetics. Design is so simple, that’s
> why it is so complicated.” —Paul Rand*

-- 
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

Reply via email to