从Android发送数组并使用Volley在PhP服务器上接收 [英] Send array from android and receive at PhP server using Volley

查看:76
本文介绍了从Android发送数组并使用Volley在PhP服务器上接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串值数组发送到PhP服务器并进行PhP解码并将其存储在PhP变量中

Hi I want to send an array of String value to PhP server and PhP decode and store them in PhP variable

这是我在android studio上的代码

Here is my code at android studio

private void getEventDetailRespond(RequestQueue requestQueue) {
        JSONObject params = new JSONObject();
        try {
            for (int i=0; i <eventIDBeacon.size();i++){
                params.put(Config.EVENT_ID, eventIDBeacon.get(i));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //Creating a JSONObject request
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,Config.DATA_URL,params.toString(),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject respond) {
                            try {
                                Toast.makeText(Beacon_MainActivity.this,"eventDetail respond "+respond.toString(),Toast.LENGTH_LONG).show();
                                eventArray = new JSONArray();
                                eventDetail = new ArrayList<>();
                                eventArray = respond.getJSONArray("result");
                                eventDetail = getEventDetail(eventArray);

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

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                            Toast.makeText(Beacon_MainActivity.this, "Unable to fetch data event Detail: " +error.getMessage(),Toast.LENGTH_LONG).show();
                        }
                    }
                );

        //Adding request to the queue
        requestQueue.add(jsonObjectRequest);

    }

    private ArrayList getEventDetail(JSONArray j) {
        ArrayList event = new ArrayList();
        //Traversing through all the items in the json array
        for (int i = 0; i < j.length(); i++) {
            try {
                //Getting json object
                JSONObject json = j.getJSONObject(i);

                //Adding the name of the event to array list
                event.add(json.getString(Config.EVENT_TITLE));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        if (event.isEmpty()) eventView.setVisibility(View.INVISIBLE);
        else {
            if (beacons.size()!=0) {
                checkIn.setVisibility(View.VISIBLE);
                eventView.setVisibility(View.VISIBLE);

                spinner.setAdapter(new ArrayAdapter<String>(Beacon_MainActivity.this, android.R.layout.simple_spinner_dropdown_item, event));
            }else {
                checkIn.setVisibility(View.INVISIBLE);
                eventView.setVisibility(View.INVISIBLE);
            }

        }
        return event;
    }

要获得PhP的大小,这是我的代码

And to recieve from PhP size, this is my code

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

// decoding the json array
$post = json_decode(file_get_contents("php://input"), true);


$eventID = $post['EventID'];



require_once('dbconnect.php');

$sql = "SELECT EventID, EventTitle, EventDesc, EventTime FROM Event WHERE EVENTID = '$eventID'";

$res = mysqli_query($con,$sql);

$result = array();


 while ($row = mysqli_fetch_array($res)){
        array_push($result,array(
            'EventID'=>$row['EventID'],
            'EventTitle'=>$row['EventTitle'],
            'EventDesc'=>$row['EventDesc'],
            'EventTime'=>$row['EventTime']
        ));
    }

        header('Content-Type: application/json');
    echo json_encode(array('result'=>$result), 256);

    mysqli_close($con);


}

似乎无法正常工作,因为我无法发送和数组到PhP服务器并在PhP服务器上对其进行解码.任何帮助都非常感谢.

It seem not to work as I can not send and array to PhP server and decode it on PhP server. any help is much appreciate.

推荐答案

您必须在getEventDetailRespond方法中覆盖凌空抽空的getParams方法

you have to override volley's getParams method inside your getEventDetailRespond method

 @Override
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
 JSONObject params = new JSONObject();
    try {
        for (int i=0; i <eventIDBeacon.size();i++){
            params.put(Config.EVENT_ID, eventIDBeacon.get(i));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
return params;
};

这是使用Volley库发送参数的方法. 在您的情况下,您必须创建以下代码:

that's the way to send params using Volley library.. in your case you have to create this:

 Map<String, ArrayList<>> params = new          
       HashMap<String,ArrayList<>>();

其中数组列表是您想要的列表

where array list is your desired list

这篇关于从Android发送数组并使用Volley在PhP服务器上接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆