代码之家  ›  专栏  ›  技术社区  ›  Sapo121

截击发送参数并获得响应

  •  0
  • Sapo121  · 技术社区  · 7 年前

    我有一个应用程序,可以从中插入和检索gps数据 mysql 使用 php api 。我在发送或获取数据方面没有问题,我正在努力解决的问题是,当我尝试将参数发送到 api (例如user\u id)我得到 Error Data 这意味着我在我的要求下做错了什么。我正试图发送 user_id ,然后将数据检索到 listview 与该id关联。我看到了一些使用 JsonArrayRequest 这可能是我应该使用的,但我在尝试使用Post方法实现它时不断出错 params

    public void test( final String id_user){
    
    
            RequestQueue requestQueue = 
    Volley.newRequestQueue(MeusTrilhos.this);
            StringRequest stringRequest = new StringRequest(Request.Method.POST, 
    urlget, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    int count = response.length();
    
                    try {
                        for(int i = 0; i < count; i++){
                        JSONObject jo = new JSONObject(response);
    
                        Trilhos tri = new Trilhos();
    
    
                        tri.setId(jo.getInt("id"));
                        tri.setTitulo(jo.getString("titulo"));
                        tri.setId_user(jo.getInt("id_user"));
                        tri.setDificuldade(jo.getString("dificuldade"));
                        TrilhosList.add(tri);
    
                        }
    
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(MeusTrilhos.this, "error data", 
    Toast.LENGTH_SHORT).show();
                        adapter.notifyDataSetChanged();
                    }
                }
    
                }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i("HttpError", "erro" + error);
                }
    
            }){
                @Override
                protected Map<String, String> getParams() throws 
    AuthFailureError {
                        Map<String, String> stringMap = new HashMap<>();
    
                        stringMap.put("id_user", id_user);
    
    
                        return stringMap;
    
                }
            };
    
            requestQueue.add(stringRequest);
        }
    }
    

    这是我的php脚本

    if($_SERVER['REQUEST_METHOD']=='POST'){ 
    $id_user = $_POST['id_user'];
    $user = "root";
    $pass = "";
    $host= "localhost";
    $dbname="check";
    
    $con = mysqli_connect($host,$user,$pass,$dbname);
    $stmt=$con->prepare("SELECT id,titulo, id_user,dificuldade FROM trilhos 
    where id_user='".$id_user."'");
    //executing the query 
    $stmt->execute();
    $stmt->bind_result($id, $titulo, $id_user, $dificuldade);
    $trilhos=array();
    while($stmt->fetch()){
        $temp=array();
        $temp['id']=$id;
        $temp['titulo']=$titulo;
        $temp['id_user']=$id_user;
        $temp['dificuldade']=$dificuldade;
        array_push($trilhos, $temp);
        }
    
    
    
    echo json_encode($trilhos);
    
    mysqli_close($con);
    }
    

    错误日志

    at org.json.JSON.typeMismatch(JSON.java:111)
    false mManagedProfileInQuietMode: false mKeyguardVisible: false 
    mCurrentUserId:0 mCurrentProfileId:0 mSecondSpaceStatusIconVisible: true 
    showIcon:false
    W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:160)
    at org.json.JSONObject.<init>(JSONObject.java:173)
    trilhosportugal.MeusTrilhos$1.onResponse(MeusTrilhos.java:135)
    trilhosportugal.MeusTrilhos$1.onResponse(MeusTrilhos.java:128)
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Sapo121    7 年前

    解决了它,尝试检索jsonobject而不是jsonarray

    public void test2(final String id_user){
    RequestQueue requestQueue = Volley.newRequestQueue(MeusTrilhos.this);
    StringRequest stringRequest=new StringRequest(Request.Method.POST, urlget,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    System.out.println(response);
                    Toast.makeText(MeusTrilhos.this, "boa entrou", 
    Toast.LENGTH_LONG).show();
                    try {
                        JSONArray array=new JSONArray(response);
                        for (int i=0; i < array.length(); i++) {
                            JSONObject jo=array.getJSONObject(i);
    
                            Trilhos tri = new Trilhos();
    
    
                            tri.setId(jo.getInt("id"));
                            tri.setTitulo(jo.getString("titulo"));
                            tri.setId_user(jo.getInt("id_user"));
                            tri.setDificuldade(jo.getString("dificuldade"));
                            TrilhosList.add(tri);
    
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
    
                    adapter.notifyDataSetChanged();
    
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MeusTrilhos.this, "erro erro", 
    Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params=new HashMap<String, String>();
    
            params.put("id_user", id_user);
    
    
            return params;
        }
    
    };
    requestQueue.add(stringRequest);
    }