gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:02:39 25 4
gpt4 key购买 nike

我需要通过创建 JSON 对象将数据从 MYSQL 数据库传递到 android 应用程序。我只能从 MYSQL 数据库传递第一个值。如何将所有值一起传递给 android 应用程序。 (我的数据库包含 10 多个位置的纬度和经度值。)

以下是我仅将第一行值从数据库传递到 android 应用程序的代码。

        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);
}

这是我的服务器端编码:

<?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();
?>

最佳答案

您需要在前一个字符串中附加下一个纬度和经度值:

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

并在 Mapview 中获取这些数组列表:

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

arrlat = getIntent().getStringArrayListExtra("returnString");
arrlan = getIntent().getStringArrayListExtra("returnString1");

关于php - 在android中使用JSON传递数据数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13926604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com