Search code examples
androidandroid-studioandroid-volleyjsonobjectrequest

Volley Get Request: onResponse is never called


im pretty new to Android Studio and I'm trying to build a Get Request using Volley, the Server response is a JsonObject. I tested the code with breakpoints but I wonder why I don't jump into onResponse or why it won't work.

Here's my Code of the Get Method:

public Account GetJsonObject(String urlExtension, String name, Context context) {
    String baseURL = "myurl.com/api";
    baseURL += urlExtension + "/" + name; 
    // url will look like this: myurl.com/api/user/"username"

    final Account user = new Account("","");
    //Account(name, email)
    RequestQueue requestQueue;
    requestQueue = Volley.newRequestQueue(context);

    JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.GET, baseURL,null,
            new Response.Listener<JSONObject>() {

                // Takes the response from the JSON request
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONObject obj = response.getJSONObject("userObject");

                        String username = obj.getString("Username");
                        String email = obj.getString("Email");
                        user.setUsername(username);
                        user.setEmail(email);

                    }
                    catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });


    requestQueue.add(jsonObject);
    return user;
    }

Solution

  • As @GVillavani82 commented your onErrorResponse() method body is empty. Try to log the error like this

     new Response.ErrorListener() {
         @Override
         public void onErrorResponse(VolleyError error) {
           Log.e("ERROR", "Error occurred ", error);   
         }
     }
    

    Make sure that you have the below permission set in AndroidManifest.xml file and the api URL is proper.

    <uses-permission android:name="android.permission.INTERNET"/>
    

    And JsonObjectRequest class returns Asynchronous network call class. Modify your code like below.

    // remove Account return type and use void
    
    public void GetJsonObject(String urlExtension, String name, Context context) {
    ....
    ....  // other stuffs
    ....
    
    JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.GET, baseURL,null,
            new Response.Listener<JSONObject>() {
    
                // Takes the response from the JSON request
                @Override
                public void onResponse(JSONObject response) {
    
                    processResponse(response);  // call to method
    
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("ERROR", "Error occurred ", error);   
                }
            });
    
          requestQueue.add(jsonObject);
    }
    

    Now create another method like below

    private void processResponse(JSONObject response) {
        try {
             final Account user = new Account("","");
             JSONObject obj = response.getJSONObject("userObject");
    
             String username = obj.getString("Username");
             String email = obj.getString("Email");
             user.setUsername(username);
             user.setEmail(email);
    
        } catch (JSONException e) {
             e.printStackTrace();
        }
    }