Shruti B
Shruti B

Reputation: 15

Volley String request Response (Listener) error

I am using Volley String GET method, I don't know why Response (Listener) is giving me error and ErrorListener is also giving error. Here is my code, please help me to find the error

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

Upvotes: 0

Views: 2540

Answers (1)

quileab
quileab

Reputation: 11

Try with this (works for me);

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

Upvotes: 1

Related Questions