[Android] นำค่าจาก DB json array ออกไปใช้ยังไงหรอคะ ?
จะดึง lat long จาก db ค่ะ เหมือนมันส่งค่าออกมาไม่ได้ค่ะ
ตรง double[] latitude = {.....}; ตรงข้างในวงเล็บถ้าใส่ตัวเลขปกติได้ค่ะ แต่ดึงจาก db ไม่ได้ค่ะ TT
โค้ดดึงค่ะ
Code (Android-Java)
class GetLocation extends AsyncTask<Void, Void, String> {
//Explicit
private DataView getLocation;
private static final String urlJSON = "http://salamander/2559/getlatlong.php";
// private Bitmap imge;
public GetLocation(DataView getLocation) {
this.getLocation = getLocation;
}
@Override
protected String doInBackground(Void... params) {
try {
OkHttpClient okHttpClient = new OkHttpClient();
Request.Builder builder = new Request.Builder();
Request request = builder.url(urlJSON).build();
com.squareup.okhttp.Response response = okHttpClient.newCall(request).execute();
return response.body().string();
} catch (Exception e) {
Log.d("26novV1", "e doIn==>" + e.toString());
return null;
}
//return null;
}//doInBack
@Override
public void onPostExecute(String s) {
Log.d("26novV1", "Json ==>" + s);
//str = s;
try {
JSONArray jsonArray = new JSONArray(s);
double max = 0;
double min = 100;
for (int i = 0; i < jsonArray.length(); i += 1) {
//Get Json from Database
JSONObject jsonObject = jsonArray.getJSONObject(i);
String strSignID = jsonObject.getString("SignID");
String strSignName = jsonObject.getString("SignName");
String strLat = jsonObject.getString("Latitude");
String strLng = jsonObject.getString("Longitude");
double[] LatArray = new double[jsonArray.length()];
LatArray[i] = Double.parseDouble(strLat);
double latt[] = {LatArray[i]};
double[] LngArray = new double[jsonArray.length()];
LngArray[i] = Double.parseDouble(strLng);
double lngg[] = {LngArray[i]};
String[] signName = new String[jsonArray.length()];
signName[i] = strSignName;
String smm[] ={signName[i]};
sm[i] = strSignName;
lat[i] = Double.parseDouble(strLat);
lng[i] = Double.parseDouble(strLng);
// sm= smm[0];
// lat= latt[0];
// lng= lngg[0];
// str = sm;
// latt = lat;
// lngg = lng;
// places = sm;
// latitudes = lat;
// longitudes = lng;
Log.d("22FebV2", "Name:" + places[i]+"lat:"+latitudes[i]+"lng:"+longitudes[i]);
}//for
// Log.d("22FebV1", "distance:" + signname);
}
catch (Exception e) {
e.printStackTrace();
}
// Log.d("22FebV2", "Name:" + str[0]+"lat:"+lat[0]+"lng:"+lng[0]);
}//onPost
}//GetLocation
ตรงที่จะเรียกใช้อะค่ะ
Code (Android-Java)
double[] latitudes = new double[]{16.464869183517187 ,16.480202875616925,16.466050472661454};
double[] longitudes = new double[]{102.81515311449766 ,102.83281110227108, 102.83200711011888};
String[] places = new String[]{"Esquermes", "Montebello",""};
ตรง lat / long / places จะเรียกใช้จาก db ค่ะ ไม่ทราบว่ามันเรียกใช้ยังไงหรอคะ ขอบคุณมากๆค่าา ^^Tag : Mobile, MySQL, Android, JAVA
Date :
2017-03-18 23:20:50
By :
ryomaconz
View :
1119
Reply :
1
น่าจะเหมือนตัวนี้ครับ
Code (PHP)
<?php
$objConnect = mysql_connect("localhost","root","root");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM `location` ORDER BY LocationID ASC ";
$objQuery = mysql_query($strSQL) or die(mysql_error());
$arrRows = array();
$arryItem = array();
while($arr = mysql_fetch_array($objQuery)) {
$arryItem["LocationID"] = $arr["LocationID"];
$arryItem["Latitude"] = $arr["Latitude"];
$arryItem["Longitude"] = $arr["Longitude"];
$arryItem["LocationName"] = $arr["LocationName"];
$arrRows[] = $arryItem;
}
echo json_encode($arrRows);
?>
=> Android Google Map : Marker Location from PHP/MySQL (JSON)
Date :
2017-03-19 21:22:03
By :
mr.win
Load balance : Server 02