Android
HttpURLConnection
从MySQL表中检索数据
我收到了这个错误:
W / System.err:org.json.JSONException:类型的值br java.lang.String无法转换为JSONObject
错误就在这一行:
JSONObject jsonObj = new JSONObject(myJSON);
我的代码:
try {
JSONObject jsonObj = new JSONObject(myJSON);
Challenges = jsonObj.getJSONArray(TAG_RESULTS);
for (int i = 0; i < Challenges.length(); i++) {
JSONObject c = Challenges.getJSONObject(i);
String id = c.getString(TAG_ID);
String trackCoordinates = c.getString(TAG_COORDINATES);
HashMap<String, String> challengesFromDB = new HashMap<>();
challengesFromDB.put(TAG_ID, id);
challengesFromDB.put(TAG_COORDINATES, trackCoordinates);
ChallengesList.add(challengesFromDB);
}
.php
档案:
$sql = "select * FROM Challenges";
$res=mysqli_query($con, $sql);
$result=$array();
while($row = mysqli_fetch_array($res)){
array_push($result,array('id'=>$row[0],'userMail'=>$row[1],'trackCoordinates'=>$row[2]));
}
header('Content-Type: application/json');
print json_encode(array("result"=>$result));
mysqli_close($con);
由于我的表格字段可能发生错误:trackCoordinates
,此处存储googleMaps纬度,长度坐标数组,例如:
[lat/lng: (54.892597770959945,23.87877881526947), lat/lng: (54.89242519582151,23.876227363944054), lat/lng: (54.8917038430203,23.877791762351986)]
答案 0 :(得分:1)
你没有获得JSON,你得到了html。您服务器上的某些内容正在输出html,或者您正在输入错误的网址。