发送和接收从凌空的PHP页面

时间:2016-07-13 15:45:36

标签: java php android json android-volley

我打算将一个参数发送到一个php页面,并且在以json格式执行特定处理后,我得到了相同的页面。我写了以下代码:

//Showing the progress dialog
final ProgressDialog loading = ProgressDialog.show(this,"در حال ارتباط با سرور","لطفا صبر کنید",false,false);
final JsonArrayRequest jsonArrReq = new JsonArrayRequest(GetFavTickets_url,
    new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray jsonArray) {
            //   Log.e("LOG", jsonArray.toString());
            for (int i = 0 ; i < jsonArray.length() ; i++)
            {
                try {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Log.e("LOG", "object : " + jsonObject.getString("name"));
                    name.add(i,jsonObject.getString("name"));
                    family.add(i,jsonObject.getString("family"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            loading.dismiss();
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            //Dismissing the progress dialog
            loading.dismiss();

            //Showing toast
            Toast.makeText(Favorite.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
            Log.e("Volley",volleyError.getMessage().toString());
        }
    }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            //Converting Bitmap to String

            //Creating parameters
            Map<String,String> params = new Hashtable<String, String>();

            //Adding parameters
            params.put("id", id);

            //returning parameters
            return params;
        }
    };

    //Creating a Request Queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue.add(jsonArrReq);
}

Volley给出了以下信息:

  

org.json.JSONException:值类型为java.lang.String的错误不能   转换为JSONArray

2 个答案:

答案 0 :(得分:0)

我的PHP代码:

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
    @$national_code = $_POST['national_code'];
        $Tickets = array(null);

        $result = mysqli_query($con,"SELECT * FROM favorites WHERE user_national_code='$national_code'");

        $count=mysqli_num_rows($result);

        while($row = mysqli_fetch_array($result))
        {
            $Tickets[] = $row['ticket_id'];    
        }

        $length = count($Tickets)-1;

        for ($i=1; $i<=$length; $i++){  

            $result = mysqli_query($con,"SELECT * FROM `ticket` WHERE id='$Tickets[i]'");

            while($row = mysqli_fetch_array($result)){
                $output[]=$row;         
            }
        }
    }else{
        echo "Error";
    }
?>

答案 1 :(得分:0)

尝试替换此

 final JsonArrayRequest jsonArrReq = new JsonArrayRequest(GetFavTickets_url, new Response.Listener()

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() ...