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条回答

    报告相同问题?

    悬赏问题

    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?