2

I need to pass data from MYSQL database to android application by creating JSON object. I am able to pass only the first value from MYSQL database. How to pass all the values together to android application. (My database contains Latitude and Longitude values of more than 10 locations.)

Following is my code for passing only the first row value from database to android app.

        try {

        HttpClient client = new DefaultHttpClient();
        URI website = new URI("http://192.168.1.15/latlonret1.php");
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response
                .getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String l = "";
        String nl = System.getProperty("line.separator");

        while ((l = in.readLine()) != null) {
            sb.append(l + nl);
        }
        in.close();
        data = sb.toString();
        // return data;
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }
        try {
        String returnString;
        String returnString1;
        JSONArray jArray = new JSONArray(data);
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            JSONObject json_data1 = jArray.getJSONObject(i);


            returnString=json_data.getString("lat") + "\n";
            returnString1=json_data1.getString("lon") + "\n";
            System.out.println(returnString);
            System.out.println(returnString1);

            Intent viewIntent =new Intent(Androidmap.this,Mapview.class);

            Bundle bundle = new Bundle();
            bundle.putString("stuff", returnString); 
            viewIntent.putExtras(bundle);

            Bundle bundle1 = new Bundle();
            bundle1.putString("stuff1", returnString1); 
            viewIntent.putExtras(bundle1); 
            startActivity(viewIntent);
      }

This is my Server side coding :

<?php
 ob_start();
 $host="localhost"; // Host name 
 $username=""; // Mysql username 
 $password=""; // Mysql password 
 $db_name="test"; // Database name 
 $tbl_name="manu"; // Table name 

 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");
 $sql = "select lat , lon from manu ";
 $result=mysql_query($sql);

 if(! $result )
 {
 die('Could not get data: ' . mysql_error());
 }

 while($row=mysql_fetch_array($result , MYSQL_ASSOC)) 
{

$output[]=$row;

}

 echo json_encode($output);
 mysql_close();
?>
3
  • How do you created serevr side using PHP ? Commented Dec 18, 2012 at 5:05
  • 1
    Can you post the JSON? It's hard to say what's going on without seeing that... Commented Dec 18, 2012 at 5:08
  • I have updated my question. Please take a look Commented Dec 18, 2012 at 5:14

2 Answers 2

2

you will need to append next lat and lon value in previous string as:

ArrayList<String> arrlan=new ArrayList<String>();
ArrayList<String> arrlat=new ArrayList<String>(); 
       for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            JSONObject json_data1 = jArray.getJSONObject(i);


            returnString  =json_data.getString("lat") + "\n";
            returnString1  =json_data1.getString("lon") + "\n";

            arrlat.add(returnString);  
            arrlan.add(returnString1);   

            System.out.println(returnString);
            System.out.println(returnString1);
      }
 Intent viewIntent =new Intent(Androidmap.this,Mapview.class);


 viewIntent.putStringArrayListExtra("returnString",arrlat);
 viewIntent.putStringArrayListExtra("returnString1",arrlan);


 startActivity(viewIntent);

and in Mapview get these arraylist as:

ArrayList<String> arrlan=new ArrayList<String>();
ArrayList<String> arrlat=new ArrayList<String>(); 

arrlat = getIntent().getStringArrayListExtra("returnString");
arrlan = getIntent().getStringArrayListExtra("returnString1");
Sign up to request clarification or add additional context in comments.

2 Comments

I need to pass all the lat and lon values from database and plot it on a single map. When i do like this every time it calls a new seperate map for marking the location. How to resolve it..
@user1844654 see my edit answer and use arrlat and arrlan arraylist for getting lan and lat in map activity
1

Try this, you need to create only one object.

    for (int i = 0; i < jArray.length(); i++) 
       {
        JSONObject json_data = jArray.getJSONObject(i);

        returnString=json_data.getString("lat") + "\n";
        returnString1=json_data.getString("lon") + "\n";
       }

1 Comment

I tried it. It is getting the first value and placing it in map, and again it is coming back to the MainActivity which called the Mapactivity. Again it gets the second value and places it in a new seperate map. I do not want to create a seperate map forevery single location. I need only one map to display all the locations stored in DB on android google map

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.