问题描述:

上午检索从MySQL表使用AndroidHttpURLConnection 和数据正在此错误:值<BR java.lang.String类型不能被转换

W/System.err: org.json.JSONException: Value br of type java.lang.String cannot be converted to 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 FIL E:

$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)] 

你没有得到JSON,你得到HTML。服务器上的任何内容正在输出HTML,或者您正在输入错误的URL。

+0

我不明白,在哪里是HTML,因为我使用PHP文件,我复制上面,文件名为:retrieveChallenges.php – Darius92

+0

嗯,我不知道,如果你真的打这个网页,因为你没有发布码。但PHP的工作方式是输出数据。通常它会输出HTML,制作一个网页。该输出被发送到客户端。很明显,你正在尝试输出JSON,这很好 - 只要它击中正确的Web端点,并且你不首先输出任何html。 –

+0

可能是我的PHP代码有问题吗?例如,如果我去切断那个.php文件,我只看到我的一半代码:'$ row [0],'userMail'=> $ row [1],' trackCoordinates' => $行[2])); } header('Content-Type:application/json');打印json_encode(数组(“结果”=> $结果)); mysqli_close($ CON); '如果我转到另一个我的php文件,我什么都看不到,我需要查看源代码以查看代码 – Darius92