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

时间:2019-06-14 06:05:35

标签: php json android-studio android-volley

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

我目前正在使用this YouTube教程中的代码。

这是提供的代码

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

这是Vconnection文件:

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

我认为这段代码之所以有效,是因为它在Android Studio的Logcat部分的onResponse函数中返回了JSON字符串,但我对为什么显示

感到困惑

“未定义的索引:第3行的C:\ xampp \ htdocs \ volley \ test.php中的数组”

当我加载server_url时。

任何帮助将不胜感激。谢谢!

0 个答案:

没有答案