排球字符串请求响应(侦听器)错误

时间:2018-09-13 06:37:36

标签: android json android-volley

我正在使用Volley String GET方法,我不知道为什么Response(监听器)给我错误,而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);
}

1 个答案:

答案 0 :(得分:0)

尝试一下(对我有用);

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