org.JSONExcepction:输入的结束于字符0

时间:2019-04-15 22:11:18

标签: php android

我想通过在数据库中使用学生ID来转到下一页,同时保留我输入的ID的学生的所有信息(类似于登录名),但其答复是:

org.JSONException:End of input at character 0 of. 

我在数据库中有3个ID,只有1个ID可以使用它答复的其他2个ID

org.JSONException:End of input at character 0 of. 

我该如何解决这个问题?

android studio代码

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_go,
            new com.android.volley.Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        String success = jsonObject.getString("success");
                        JSONArray jsonArray = jsonObject.getJSONArray("student");

                        if (success.equals("1")) {

                            for (int i=0; i< jsonArray.length(); i++) {
                                JSONObject object = jsonArray.getJSONObject(i);

                                String studname = object.getString("name").trim();
                                String studphone = object.getString("phone").trim();
                                String studadd = object.getString("add").trim();
                                String studemail = object.getString("email").trim();
                                String carid = object.getString("car").trim();

                                studentSession.createSession(studid, studname, studphone, studadd, studemail, carid);

                                Intent intent = new Intent(StaffPage.this, FineTicket.class);
                                startActivity(intent);
                            }

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        gobtn.setVisibility(View.VISIBLE);
                        Toast.makeText(StaffPage.this, "Error!"+e.toString(), Toast.LENGTH_SHORT).show();
                    }

PHP代码

if ($_SERVER['REQUEST_METHOD']=='POST') {

studentid = $_POST['studid'];

require_once 'init.php';

$sql = "SELECT * FROM student WHERE student_id = '$studentid';

$response = mysqli_query($conn, $sql);

$result = array();
$result['student'] = array();


if ( mysqli_num_rows($response) == 1 ) {

    $row = mysqli_fetch_assoc($response);

    if($studentid == $row['student_id']) {

        $index['name'] = $row['student_name'];
        $index['phone'] = $row['student_phone'];
        $index['add'] = $row['student_add'];
        $index['email'] = $row['student_email'];
        $index['car'] = $row['car_id'];

        array_push($result['student'], $index);

        $result['success'] = "1";
        echo json_encode($result);

        mysqli_close($conn);

} else {

        $result['success'] = "0";
        echo json_encode($result);

        mysqli_close($conn);

    }

} 

android studio中的学生会话代码

SharedPreferences sharedPreferences;
public SharedPreferences.Editor editor;
public Context context;
int PRIVATE_MODE = 0;

private static final String PREF_NAME = "LOGIN";
private static final String LOGIN = "IS_LOGIN";
public static final String STUDID = "STUDID";
public static final String STUDNAME = "STUDNAME";
public static final String STUDPHONE = "STUDPHONE";
public static final String STUDADD = "STUDADD";
public static final String STUDEMAIL = "STUDEMAIL";
public static final String CARID = "CARID";


public StudentSession(Context context){
    this.context = context;
    sharedPreferences = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = sharedPreferences.edit();
}

public void createSession (String studid, String studname, String studphone, String studadd, String studemail, String carid){
    editor.putBoolean(LOGIN, true);
    editor.putString(STUDID, studid);
    editor.putString(STUDNAME, studname);
    editor.putString(STUDPHONE, studphone);
    editor.putString(STUDADD, studadd);
    editor.putString(STUDEMAIL, studemail);
    editor.putString(CARID, carid);
    editor.apply();
}

public boolean isLogin(){
    return sharedPreferences.getBoolean(LOGIN, false);
}

public void checkLogin(){
    if (!this.isLogin()){
        Intent i = new Intent(context, StaffPage.class);
        context.startActivity(i);
        ((FineTicket)context).finish();
    }
}

public HashMap<String, String> getStudentDetail(){

    HashMap<String, String> student = new HashMap<>();
    student.put(STUDID, sharedPreferences.getString(STUDID, null));
    student.put(STUDNAME, sharedPreferences.getString(STUDNAME, null));
    student.put(STUDPHONE, sharedPreferences.getString(STUDPHONE, null));
    student.put(STUDADD, sharedPreferences.getString(STUDADD, null));
    student.put(STUDEMAIL, sharedPreferences.getString(STUDEMAIL, null));
    student.put(CARID, sharedPreferences.getString(CARID, null));

    return student;
}

0 个答案:

没有答案