douwei1904 2019-06-14 06:05
浏览 138

将JSON字符串从Android Studio发送到PHP

I want to send a JSON string from Android Studio to PHP.

I'm currently using the code from this youtube tutorial.

This was the code provided

final String server_url="http://192.168.0.23:81/volley/test.php?";


StringRequest stringRequest=new StringRequest(Request.Method.POST, server_url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response)
                    {
                        Log.d("response", "result : "+response); //when response come i will log it
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error)
                    {
                        error.printStackTrace();
                        error.getMessage(); // when error come i will log it
                        Log.d("ERROR", error.getMessage());
                    }
                }
        )
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String,String> param=new HashMap<String, String>();
                param.put("array", new Gson().toJson(dataList)); // array is key which we will use on server side
                param.put("hello", "hi");

                return param;
            }


        };

        stringRequest.setShouldCache(false);
        Vconnection.getnInstance(this).addRequestQue(stringRequest);

and this is the Vconnection file:

    private static Vconnection nInstance;
    private RequestQueue RQ;
    private Context CTX;

    private Vconnection(Context context)
    {
        CTX=context;
        RQ=getRequestQue();
    }

    public RequestQueue getRequestQue()
    {
        if(RQ==null)
        {
            RQ= Volley.newRequestQueue(CTX.getApplicationContext());
        }
        return RQ;
    }
    public static synchronized Vconnection getnInstance(Context context)
    {
        if(nInstance==null)
        {
            nInstance=new Vconnection(context);
        }
        return nInstance;
    }
    public <T> void addRequestQue(Request<T> request)
    {
        RQ.add(request);
    }

I think this code works because it returns the JSON string as a result in the onResponse function in the Logcat part of Android Studio but I'm confused as to why it shows

"Undefined index: array in C:\xampp\htdocs\volley\test.php on line 3"

when I load the server_url.

Any help would be much appreciated. Thank you!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 服务器一块硬盘重启过程中安装后,硬盘不识别了怎么修复?
    • ¥15 微信红包不拆包 知道红包金额
    • ¥15 0基础学机器人运动控制要多久?
    • ¥15 .net core 怎么进行中英文转换
    • ¥15 数学的三元一次方程求解
    • ¥20 iqoo11 如何下载安装工程模式
    • ¥15 本题的答案是不是有问题
    • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
    • ¥15 C++使用Gunplot
    • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?