无法通过空间获得JsonObjectRequest

时间:2016-08-10 14:15:51

标签: php android json android-volley

我想从我的php脚本中获取JSONObject到android。一切顺利,但如果用户输入他的名字或姓名与空间应用程序crused。

exmple: JACK //它的o.k 初级史密斯//粉碎

这是我的php脚本:

<?php
require('connection.php');

$phone = $_GET['phone'];
$password = $_GET['password'];

$sql = ("select * from Users where phone = '$phone' and password = '$password' ");

$check = mysqli_fetch_array(mysqli_query($con,$sql));

if(isset($check)){
    $hasApartment = mysqli_query($con,$sql);
    $row = mysqli_fetch_assoc($hasApartment);
    $apartmentNumber = $row['apartmentNum'];
        $firstName = $row['firstName'];
        $lastName = $row['lastname'];

        $image = $row['image'];

    if($apartmentNumber > 0){
    echo '{"query_result":"SUCCESS", "apartmentNumber":' . $apartmentNumber.' , "firstName":' . trim($firstName) .' ,"lastName":' . trim($lastName).', "image":"' . $image.'"}';

        }
else{
echo '{"query_result":"noAparetemet"}';
}
}
else{
    echo '{"query_result":"FAILURE"}';
}

mysqli_close($con);

?>

这是我的android:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(final JSONObject response) {

                    try {
                        String query_result = response.getString("query_result");
                        switch (query_result){
                            case "SUCCESS": // user name and password exists and mathces and user associated with apartment
                                SharedPreferences.Editor editor = preferences.edit();
                                RoomateModel roomateModel = new RoomateModel(response);
                                Gson gson = new Gson();
                                String json = gson.toJson(roomateModel);
                                editor.putString("USER", json);
                                editor.commit();
                                Intent intent = new Intent(LoginActivityScreen.this, HomeActivityScreen.class);
                                editor.putBoolean("loggedIn", true);
                                editor.apply();
                                startActivity(intent);
                                break;

                            case "FAILURE":  //user name or password are incorrect
                                loginLayout.setVisibility(View.VISIBLE);
                                logging.setVisibility(View.GONE);
                                Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();
                                break;
                            default:
                                break;
                        }

                        } catch (JSONException e) { //this exception is caught
                        e.printStackTrace();
                        loginLayout.setVisibility(View.VISIBLE);
                        logging.setVisibility(View.GONE);
                        Log.e("e" , e.toString()+e.getMessage());
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            loginLayout.setVisibility(View.VISIBLE);
            logging.setVisibility(View.GONE);
            Log.e("e" , error.toString()+error.getMessage());
        }

    });
    MySingleton.getInstance(context).addToRquestQueue(jsonObjectRequest);
}

这是我的日志:

com.android.volley.ParseError: org.json.JSONException: Unterminated object at character 85 of 
{"query_result":"SUCCESS", "apartmentNumber":63 , "firstName":JACK¨ ,"lastName":junior smith, "image":"url.jpeg"}  

1 个答案:

答案 0 :(得分:1)

问题是JSON响应中的名字和姓氏中缺少引号。你应该

"lastName": "junior smith"

而不是

"lastName":junior smith

尝试替换

echo '{"query_result":"SUCCESS", "apartmentNumber":' . $apartmentNumber.' , "firstName":' . trim($firstName) .' ,"lastName":' . trim($lastName).', "image":"' . $image.'"}';

通过

echo '{"query_result":"SUCCESS", "apartmentNumber":' . $apartmentNumber.' , "firstName":"' . trim($firstName) .'" ,"lastName":"' . trim($lastName).'", "image":"' . $image.'"}';