如何在没有php的情况下使用java在android中使用volley检索mysql数据?

时间:2016-09-06 05:56:22

标签: android mysql performance android-volley

如何在没有PHP的情况下使用Android中的Volley with Java检索MySql数据?

1 个答案:

答案 0 :(得分:0)

要与服务器通信,您必须在java中编写API。其中包括数据库名称,用户名,访问密码,sql查询以获取数据。

StringRequest stringRequest = new StringRequest(Request.Method.POST, DB_URL,

                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // responce may be String, JSONOnject, JSONArray. Handle with your own actions.
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // Error occurs in API(loop,TimeOutError,...)
                    }
                }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError  {
                Map<String, String> params = new HashMap<>();
                // Map your Key and Value to match in the API to perform actions.
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        requestQueue.add(stringRequest);