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 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突