Home > Software engineering >  Why isn't Android Volley request working?
Why isn't Android Volley request working?

Time:02-03

I simply trying to get JSON data from url (https://krokapp.by/api/rest/tags/?format=json):

RequestQueue mRequestQueue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRequestQueue = Volley.newRequestQueue(this);
    String url = "https://krokapp.by/api/rest/tags/?format=json";

    final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, response -> {
                Log.d("krok_data", response.toString());
            }, error -> {
                Log.d("krok_data", "Error");
                error.printStackTrace();
            });
    mRequestQueue.add(request);
}

However, when I try this with different JSON api, for example: https://samples.openweathermap.org/data/2.5/weather?id=2172797&appid=b6907d289e10d714a6e88b30761fae22 everything works fine. Is there something wrong with the API I'm trying to get data from?

CodePudding user response:

Since the ressource you are calling returns a JSON array, simply change the volley request type from JsonObectRequest to JsonArrayRequest.

Should look like this then:

RequestQueue mRequestQueue = Volley.newRequestQueue(this);
String url = "https://krokapp.by/api/rest/tags/?format=json";

    final JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
            response -> Log.d("krok_data", response.toString()),
            error -> {
                Log.d("krok_data", "Error");
                error.printStackTrace();
    });
    mRequestQueue.add(request);
  • Related