首页 > 解决方案 > Volley String 请求响应(侦听器)错误

问题描述

我正在使用 Volley String GET 方法,我不知道为什么 Response (Listener) 给我错误,而 ErrorListener 也给错误。这是我的代码,请帮我找出错误

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

private void getData(){
    //Creating a string request

    StringRequest stringRequest = new StringRequest(Request.Method.GET,Config.DATA_URL,new Response.Listner<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject j = null;
                    try {
                        //Parsing the fetched Json String to JSON Object
                        j = new JSONObject(response);

                        //Storing the Array of JSON String to our JSON Array
                        result = j.getJSONArray(Config.JSON_ARRAY);

                        //Calling method getStudents to get the students from the JSON Array
                        getStudents(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

            //Creating a request queue
            RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

            //Adding request to the queue
            requestQueue.add(stringRequest);
}

标签: androidjsonandroid-volley

解决方案


试试这个(对我有用);

           final RequestQueue requestQueue= Volley.newRequestQueue(MainActivity.this);
    StringRequest stringRequest=new StringRequest(Request.Method.POST, serverURL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // Do something with response string
                    tv_respuesta.setText(response);
                    requestQueue.stop();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // Do something when get error
                    tv_respuesta.setText(error.toString());
                    requestQueue.stop();
                }
            }

    );
    requestQueue.add(stringRequest);

推荐阅读