com.android.volley.ParseError:org.json.JSONException:Value <br of =“”type =“”java.lang.string =“”can not =“”be =“”converted =“”to =“”jsonarray

时间:2017-01-09 18:30:25

标签: php android android-volley

=“”

这是我的问题:

我试图通过PHP在服务器上获取数据,每当我尝试这样做时,我都会收到此错误:

[com.android.volley.ParseError:org.json.JSONException:java.lang.String类型的值br无法转换为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"
  }
]

我使用POSTMAN应用程序来检查我的php是否正常工作,而且确实如此。

我无法用我的代码找到问题。

任何形式的帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

试试这样。

JSONARRAY array = new JSONARRAY(response);

然后在for循环中使用数组。