com.android.volley.ParseError:org.json.JSONException:值
问题描述:
这里是我的问题:com.android.volley.ParseError:org.json.JSONException:值<java.lang.String类型的BR不能转换到JSONArray
我试图通过对从服务器获取数据在Android中的PHP和每当我尝试做到这一点,我得到这个错误:
[com.android.volley.ParseError:org.json.JSONException:值br的类型java.lang.String不能转换为JSONArray]
我确定我的代码是正确的,因为我试图使用https://jsonplaceholder.typicode.com/ 的数据,它工作。
我正在使用xampp服务器并使用php进行连接。
这里是我的Java代码:
final String url1 = "http://192.168.0.101/deliveryApp/sellerProfile.php";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url1, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.v("response is : ", String.valueOf(response));
for (int i = 0 ; i < response.length() ; i++){
try{
JSONObject jsonObject = response.getJSONObject(i);
String tvfirstname = jsonObject.getString("firstname");
String tvlastname = jsonObject.getString("lastname");
String tvphonenumber = jsonObject.getString("email");
String tvemail = jsonObject.getString("phonenumber");
sellername.setText(tvfirstname +" "+ tvlastname);
selleremail.setText(tvemail+" ");
sellerphonenumber.setText(tvphonenumber+" ");
}catch (JSONException e){
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error);
}
});
requestQueue.add(jsonArrayRequest);
这我的PHP脚本:
<?php
$dbname = "delivery_app";
$username = "root";
$password = "";
$host = "localhost";
$connection=mysqli_connect($host,$username,$password,$dbname);
session_start();
$email = $_SESSION['tempemail'];
//$email = $_POST["email"];
$querySellerProfile = "SELECT * FROM login WHERE email = '$email'";
if($fetchresult = mysqli_query($connection,$querySellerProfile)){
while ($response = mysqli_fetch_array($fetchresult)) {
// echo $response['firstname'];
// echo $response['lastname'];
// echo $response['email'];
// echo $response['phonenumber'];
// echo $response['password'];
$response2[] = $response;
}
echo json_encode($response2);
}else{
die("failed to get profile information");
}
/*
THIS CODE WILL RETRIEVE USER DATILS , AND ITS WORKING PERFECTLY
*/
?>
,并从服务器的响应是:
[
{
"0": "2",
"1": "testfname",
"2": "testlname",
"3": "testemail",
"4": "9650684491",
"5": "5555",
"id": "2",
"firstname": "testfname",
"lastname": "testlname",
"email": "testemail",
"phonenumber": "9650684491",
"password": "5555"
}
]
我用邮差应用检查我的PHP是否在工作,它确实。
我只是找不到我的代码的问题。
任何形式的帮助,将不胜感激。
答
试试这个。
JSONARRAY array = new JSONARRAY(response);
然后在for循环中使用数组。
请您详细说明?? –