簡體   English   中英

android volley post參數並接收json

[英]android volley post parameter and receive json

我對 android 開發還很陌生,我正在嘗試發出一個 android volley 請求,該請求將一個 ID 發送到 php 服務,如果它在服務器端匹配,它應該在 json 中返回與其關聯的所有值,然后在android 應用程序並顯示。 任何幫助將不勝感激

<?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 {
}

?>

安卓凌空請求

 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);

問題是,您有一個POST請求,不幸的是,在 volley 中沒有直接constructor來發送JsonArrayPOST請求。

android volley team 可能會在以后添加它,但現在你可以這樣做:

第 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);
    }
}

第 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);

此外,您需要在 url 中傳遞此 ID。

如果您需要問任何其他問題,請告訴我。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM