我试图使用post方法从volley中获取数据,但这不是取得Android的但是对于html表单它正在工作

时间:2018-06-05 20:48:21

标签: php android android-volley

我试图在我的项目中使用volley来处理我的所有HTTP请求,但是post请求失败了。 我尝试过很多解决方案,但没有成功。我想应该有一个简单而标准的方法来使用凌空与PHP。所以我想知道在我的PHP代码中接收volley发送的json对象需要做什么。

<?php

$con = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
$state = $_POST['state'];
echo $state;
$sql= "select * from $state ORDER BY shr_date DESC;";
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error() . "\n";
}
else
 {
    $result=mysqli_query($con,$sql);
    $response=array();
    while($row=mysqli_fetch_array($result))
    {
        array_push($response,array("shr_uniname"=>$row[5],"shr_heading"=>$row[1],"shr_state"=>$row[3],"shr_date"=>$row[4]));

    }
        header('Content-Type: application/json');
    echo json_encode(array("server_response"=>$response));
    mysqli_close($con);
 }

?>

Volley Post Request的Android代码。

 StringRequest stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject  jsonObject = new JSONObject(response);
                            JSONArray jsonArray=jsonObject.getJSONArray("server_response");
                            for(int i=0;i<jsonArray.length();i++)
                            {
                                JSONObject jsonObject1=jsonArray.getJSONObject(i);
                                StateModel stateModel=new StateModel(jsonObject1.getString("shr_uniname"),
                                        jsonObject1.getString("shr_state"),jsonObject1.getString("shr_heading"),
                                        jsonObject1.getString("shr_date")
                                );
                                list.add(stateModel);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            requestQueue.stop();
                        }
                        stateAdapter=new StateAdapter(StateActivity.this,list);
                        recyclerView.setAdapter(stateAdapter);
                        requestQueue.stop();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        requestQueue.stop();
                    }
                }){
                    @Override
                    protected Map<String,String> getParams()throws AuthFailureError {
                        Map<String,String> params = new HashMap<String, String>();
                        params.put("state","rajasthan");
                        return params;
                    }
                    @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");
                        return headers;
                    }

                };
                requestQueue.add(stringRequest);
            }

0 个答案:

没有答案