简体   繁体   English

android volley post参数并接收json

[英]android volley post parameter and receive json

i am fairly new to android development, i am trying to make a android volley request which send an ID to the php service and if it matches on the server side it should return all the value associated to it in json which is then recieved on the android app and displayed.我对 android 开发还很陌生,我正在尝试发出一个 android volley 请求,该请求将一个 ID 发送到 php 服务,如果它在服务器端匹配,它应该在 json 中返回与其关联的所有值,然后在android 应用程序并显示。 any help will appreciated任何帮助将不胜感激

<?php

require("include/config.php");

if (!empty($_POST)) {

    $query = " SELECT * FROM music WHERE musicID = :ID  " ;

    $query_params = array(
        ':ID' => $_POST['ID'],


            );

    try {
        $statement   = $db->prepare($query);
        $result = $statement->execute($query_params);
    }
    catch (PDOException $ex) {


        $response["success"] = 0;
        $response["message"] = "  error";
        die(json_encode($response));

    }

    $result = $statement->fetchAll();

    if($result){

            $return =array();

            foreach($result as $rows){
        $json = array();
        $json["title"] = $rows["title"];
        $json["rating"]= $rows["rating"];
        $json["Price"] = $rows["Price"];
        $json["Description"] = $rows["Description"];

        array_push ($return,$json);



            }
     echo stripcslashes(json_encode($datareturn, JSON_PRETTY_PRINT));








    }




} else {
}

?>

android volley request安卓凌空请求

 JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                for(int i=0;i<response.length();i++){
                    try{
                        JSONObject obj=response.getJSONObject(i);
                        Item item=new Item();
                        item.setTitle(obj.getString("title"));
                        item.setImage(obj.getString("image"));


                        //add to array
                        array.add(item);
                    }catch(JSONException ex){
                        ex.printStackTrace();
                    }
                }
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        AppController.getmInstance().addToRequesQueue(jsonArrayRequest,ID);

The problem is, you have a POST request, and unfortunately, there is no direct constructor in volley to send the POST request for JsonArray .问题是,您有一个POST请求,不幸的是,在 volley 中没有直接constructor来发送JsonArrayPOST请求。

the android volley team maybe add it later but for now you can do this : android volley team 可能会在以后添加它,但现在你可以这样做:

Step 1 : Create your class and extends JsonArrayRequest then you should override.第 1 步:创建您的类并扩展JsonArrayRequest然后您应该覆盖。

public class CustomJsonArrayRequest extends JsonArrayRequest {
    public CustomJsonArrayRequest(int method, String url, JSONObject jsonRequest, Listener<JSONArray> listener, ErrorListener errorListener) {
        super(method, url, jsonRequest, listener, errorListener);
    }
}

Step 2 : Now that you have a constructor to send the request you can easily send your request using it.第 2 步:现在您有了一个发送请求的构造函数,您可以使用它轻松发送您的请求。

//If you are sending ID inside request body
JsonObject requestJson = new JsonObject();
requestJson.put("ID", "Your ID Value");

CustomJsonArrayRequest jsonArrayRequest=new CustomJsonArrayRequest(RequestMethod.POST,URL, requestJson, Response.Listener<JSONArray>() {
                @Override 
                public void onResponse(JSONArray response) {

            for(int i=0;i<response.length();i++){
                try{ 
                    JSONObject obj=response.getJSONObject(i);
                    Item item=new Item();
                    item.setTitle(obj.getString("title"));
                    item.setImage(obj.getString("image"));


                    //add to array 
                    array.add(item);
                }catch(JSONException ex){
                    ex.printStackTrace();
                } 
            } 
            adapter.notifyDataSetChanged(); 
        } 
    }, new Response.ErrorListener() {
        @Override 
        public void onErrorResponse(VolleyError error) {

        } 
    }); 
    AppController.getmInstance().addToRequestQueue(jsonArrayRequest);

Also, you need to pass this ID in the url.此外,您需要在 url 中传递此 ID。

Let me know if you need to ask anything else.如果您需要问任何其他问题,请告诉我。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM