package com.joshbradley.pokemonapp.activities;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.joshbradley.pokemonapp.R;
import com.joshbradley.pokemonapp.adapters.Adapter;
import com.joshbradley.pokemonapp.helperclass.HelperClass;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class HomeScreen extends AppCompatActivity {
//VARIABLES
RecyclerView recyclerView;
List<HelperClass> helperClasses;
Adapter adapter;
private static String JSON_URL_TEST = "https://pokeapi.co/api/v2/pokemon?limit=151";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
//RECYCLER AND VIEW
recyclerView = findViewById(R.id.home_screen_recycler);
helperClasses = new ArrayList<>();
//CALLING EXTRACT DATA METHOD extractData();
}
//EXTRACT DATA & REQUESTS
private void extractData() {
RequestQueue queue = Volley.newRequestQueue(this);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, "https://pokeapi.co/api/v2/pokemon?limit=151", null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++){
try {
JSONObject jsonObject = response.getJSONObject(i);
HelperClass helperClass = new HelperClass();
helperClass.setName(jsonObject.getString("name"));
helperClass.setImageUrl(jsonObject.getString("url"));
helperClasses.add(helperClass);
} catch (JSONException e) {
e.printStackTrace();
}
}
//ADAPTER AND LAYOUT MANAGER
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
adapter = new Adapter(getApplicationContext(), helperClasses);
recyclerView.setAdapter(adapter);
}
// ERROR RESPONSE
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(jsonArrayRequest);
}
}
// MY ERROR
com.android.volley.ParseError: org.json.JSONException: Value {"count":1154,"next":"https:\/\/pokeapi.co\/api\/v2\/pokemon?offset=151&limit=151","previous":null,"results":[{"name":"bulbasaur","url":"https:\/\/pokeapi.co\/api\/v2\/pokemon\/1\/"},{"name":"ivysaur","url":"https:\/\/pokeapi.co\/api\/v2\/pokemon\/2\/"},{"name":"venusaur","url":"https:\/\/pokeapi.co\/api\/v2\/pokemon\/3\/"},
In your extractData() method you have the following which implies that you are expecting the service response to be a JSONArray:
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, ...
If you look at the body of the Exception you will see that the service is returning an object (which contains an array).
{ "count":1154
, "next":"https:\/\/pokeapi.co\/api\/v2\/pokemon?offset=151&limit=151"
, "previous":null
, "results":[ ... some array of values]
... you cut it off so there may be more fields ...
}
As such, it looks like you should accept a JSONObject and then extract the value associated with the results
key. I'm not sure on the syntax but probably something like...
JsonObjectRequest request = new JsonObjectRequest(...
@Override
public void onResponse(JSONObject response) {
JSONArray results = response.getJSONArray("results");
....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.