在VOLLEY中发送帖子并同时获得请求

时间:2018-07-06 07:03:55

标签: php android mysql android-volley

我有一个recyclerview,它向用户显示了我的价值观。但是我需要向他们展示自己的价值观。

首先,这是GET值的代码。 (工作正常)

 private void loadValues() {

    StringRequest stringRequest=new StringRequest(Request.Method.GET, urlUpload, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            try {
                JSONArray values =new JSONArray(response);
                for (int i = 0; i < values.length(); i++) {
                    JSONObject object=values.getJSONObject(i);

                    int id =object.getInt("id");
                    int user_id=object.getInt("user_id");

                    String department=object.getString("department");
                    String description=object.getString("description");
                    String address=object.getString("address");
                    String addressdesc=object.getString("addressdesc");
                    String lattitude=object.getString("lattitude");
                    String longitude=object.getString("longitude");
                    String image=object.getString("image");
                    String state=object.getString("state");

                    Modelmodel=new Model(id,user_id,department,description,address,addressdesc,lattitude,longitude,image,state);
                    recyclerList.add(model);
                }
                adapter=new Adapter(UserRecords.this,recyclerList);
                recyclerView.setAdapter(adapter);

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

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            Toast.makeText(UserRecords.this, error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String,String> params =new HashMap<>();
            params.put("user_id","4");  // 4 is just for now. it should return 3 values from sql.
            return params;
        }
    } ; 
    Volley.newRequestQueue(this).add(stringRequest);

}

那是我的GET的PHP脚本。而且它的工作也很好。当我尝试$user_id2 =4;
它会向我返回值。

<?php 

 //  $user_id2  =  $_POST['user_id'];
 require_once 'connect.php';
 mysqli_query($conn,"SET NAMES 'utf8'");

 //Checking if any error occured while connecting
 if (mysqli_connect_errno()) {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 die();
 }

 //creating a query

 $stmt = $conn->prepare("SELECT records.id, records.user_id, parameters.p_name AS department, records.description, 
 records.address, records.addressdesc, records.lattitude, records.longitude, records.image, records.state 
 from records,parameters WHERE parameters.p_id=records.department AND parameters.group_id=
 (SELECT groups.id FROM groups where groups.group_name = 'department') AND records.user_id = '$user_id2'");

 //executing the query 
 $stmt->execute();

 //binding results to the query 
 $stmt->bind_result($id,$user_id,$department,$description,$address,$addressdesc,
 $lattitude,$longitude,$image,$state);

 $records = array(); 

 //traversing through all the result 
 while($stmt->fetch()){
 $temp = array();
 $temp['id'] = $id; 
 $temp['user_id'] = $user_id; 
 $temp['department'] = $department; 
 $temp['description'] = $description; 
 $temp['address'] = $address; 
 $temp['addressdesc'] = $addressdesc; 
 $temp['lattitude'] = $lattitude; 
 $temp['longitude'] = $longitude; 
 $temp['image'] = $image; 
 $temp['state'] = $state; 
 array_push($records, $temp);
 }

 //displaying the result in json format 
 echo json_encode($records);

我在Android上执行GET请求之前先执行POST请求,但没有成功。 如果您查看我的SQL代码的结尾,您将看到records.user_id = '$user_id2'。我需要通过POST请求获取user_id2值,然后需要将此值提供给SQL。然后,我将获取请求,并在recyclerview中显示人们的价值观。

简而言之:我需要将user_id发送到PHP,然后在SQL中使用它,然后将带有get方法的值返回给Android。

1 个答案:

答案 0 :(得分:0)

对不起,我刚刚在Request.Method中看到了一些东西。

我没有更改此代码中的任何内容,只是将Request.Method.GET更改为Request.Method.DEPRECATED_GET_OR_POST

我现在刚刚看到了这种方法。因此,它运行完美。

相关问题