3

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

1 Answers1

1

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();
    }
}
Shashanth
  • 4,995
  • 7
  • 41
  • 51
  • ah okay, thank you and @GVillavani82 for the help :D –  Apr 23 '17 at 10:59
  • If my answer helped to fix your problem please consider my as accepted by checking right mark left to my answer. – Shashanth Apr 23 '17 at 12:23
  • unfortunately, now i had time to try it out but it doesn't work, it jumps in the ErrorListener.. I am using the proper url –  Apr 23 '17 at 17:24
  • Sorry, if this is a stupid question, where can i see the error? I was running debug mode and didn't find any error. –  Apr 23 '17 at 17:40
  • Log the errors as I said in my post. And on bottom of Android Studio you will get Android monitor there set the log level to Error. Now you can trace the errors. Or put a break point inside onErrorResponse() method. Now you're good to go. – Shashanth Apr 23 '17 at 17:44
  • 1
    Alright, thank you, I'll try it on my own now, thank you :) –  Apr 23 '17 at 17:57