使用params使用volley获取null数据

时间:2016-02-19 09:44:04

标签: php android android-volley android-json jsonobjectrequest

private void fetchPatients() {
    // Creating volley request obj
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put("uid", "P001");

    JsonObjectRequest req = new JsonObjectRequest(url, new JSONObject(headers),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());

                    for (int i = 0; i < response.length();i++) {
                        try {
                            //Log.d("id","hi1") ;
                            //Log.d("i","i:"+i);

                            AppointmentList appointment = new AppointmentList();

                            //Log.d("length", "length:" + response.length());
                            JSONObject objuid= response.getJSONObject("uid");

                            JSONObject objatype= response.getJSONObject("appointment_type");
                            appointment.setAtype(objatype.getString("appointment_type"));

                            JSONObject objdoctor= response.getJSONObject("doctor");
                            appointment.setDoctor(objdoctor.getString("doctor"));

                            JSONObject objdate= response.getJSONObject("date");
                            appointment.setDate(objdate.getString("date"));

                            JSONObject objtime= response.getJSONObject("time");
                            appointment.setTime(objtime.getString("time"));


                            JSONObject objqueue= response.getJSONObject("queue");
                            appointment.setQueue(objqueue.getInt("queue"));

                            appointmentList.add(appointment);

                            // updating offset value to highest value
                            //if (i >= offSet)
                            //  offSet = i;

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    mAdapter.notifyDataSetChanged();

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hidePDialog();
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();

        }

    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json; charset=utf-8");
            headers.put("User-agent", "My useragent");
            return headers;
        }
    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(req);
    hidePDialog();
}

appointmentlist_patient.php

<?php
$host = "localhost"; // host of MySQL server
$user = "root"; // MySQL user
$pwd = ""; // MySQL user's password
 $db = "ecare"; // database name

// Create connection
$con = mysqli_connect($host, $user, $pwd, $db);

// Check connection
if(mysqli_connect_errno($con)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
} 
 $server_ip = gethostbyname(gethostname());

error_reporting(0);
// array for JSON response
$json = file_get_contents('php://input');
$response = array();
$data = json_decode($json);
$hi = $data->{'uid'};

 $sql = "SELECT uid,appointment_type,doctor,date,time,queue from appointment Where uid = ".$hi;

 $res = mysqli_query($con,$sql);

 $result = array(); 

 while($row = mysqli_fetch_array($res)){
 array_push($result,array('uid'=>$row['uid']));
 array_push($result,array('appointment_type'=>$row['appointment_type']));
 array_push($result,array('doctor'=>$row['doctor']));
 array_push($result,array('date'=>$row['date']));
 array_push($result,array('time'=>$row['time']));
 array_push($result,array('queue'=>$row['queue']));
 }

 echo json_encode($hi);

 mysqli_close($con);

我想将params传递给php,但是我在php中获得的数据$ hi是NULL。 $结果也是空的。我已经多次检查但我不知道问题出在哪里。请给我一些帮助来解决这个问题。谢谢。

0 个答案:

没有答案