How to manipulate data after its retrieved via remote database

Posted by bMon on Stack Overflow See other posts from Stack Overflow or by bMon
Published on 2011-01-11T00:46:49Z Indexed on 2011/01/11 0:53 UTC
Read the original article Hit count: 220

Filed under:
|
|
|

So I've used code examples from all over the net and got my app to accurately call a .php file on my server, retrieve the JSON data, then parse the data, and print it.

The problem is that its just printing to the screen for sake of the tutorial I was following, but now I need to use that data in other places and need help figuring out that process.

The ultimate goal is to return my db query with map coordinates, then plot them on a google map. I have another app in which I manually plot points on a map, so I'll be integrating this app with that once I can get my head around how to correctly manipulate the data returned.

        public class Remote extends Activity {
    /** Called when the activity is first created. */

       TextView txt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Create a crude view - this should really be set via the layout resources
        // but since its an example saves declaring them in the XML.
        LinearLayout rootLayout = new LinearLayout(getApplicationContext());
        txt = new TextView(getApplicationContext());
        rootLayout.addView(txt);
        setContentView(rootLayout);

        // Set the text and call the connect function.
        txt.setText("Connecting...");
        //call the method to run the data retreival
        txt.setText(getServerData(KEY_121));


    }
    public static final String KEY_121 = "http://example.com/mydbcall.php";

    private String getServerData(String returnString) {

       InputStream is = null;

       String result = "";
        //the year data to send
        //ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        //nameValuePairs.add(new BasicNameValuePair("year","1970"));

        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(KEY_121);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }

        //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }
        //parse json data

        try{
                JSONArray jArray = new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                        Log.i("log_tag","longitude: "+json_data.getDouble("longitude")+
                                ", latitude: "+json_data.getDouble("latitude")
                        );
                        //Get an output to the screen
                        returnString += "\n\t" + jArray.getJSONObject(i);
                }
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }

        return returnString;
    }   

    }

So the code:

returnString += "\n\t" + jArray.getJSONObject(i);

is what is currently printing to the screen.

What I have to figure out is how to get the data into something I can reference in other spots in the program, and access the individual elements ie:

double longitude = jArray.getJSONObject(3).longitude; 

or something to that effect..

I figure the class getServerData will have to return a Array type or something?

Any help is appreciated, thanks.

© Stack Overflow or respective owner

Related posts about java

Related posts about android