doukuipei9938 2012-12-18 05:03
浏览 423
已采纳

在android中使用JSON传递数据数组

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") + "
";
            returnString1=json_data1.getString("lon") + "
";
            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();
?>
  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 如何修改为正确的?求解决
      • ¥15 django访问管理员界面问题
      • ¥20 python homework完成
      • ¥20 求解 多变量系统的最小二乘辨识问题的推导以及matlab仿真
      • ¥15 arduino esp8266 Blinker编译报问题
      • ¥15 ubuntu18.04运行模型,直接死机
      • ¥30 (问卷调查)莫名其妙丢了u盘,你们会是什么心理状态
      • ¥100 Spark+android应⽤案例
      • ¥15 yolov8 目标检测 重叠 遮挡
      • ¥20 微信聊天记录如何部署到服务器上