Hello stackoverflow community!!
How do I access a JSON array in this URL “https://api.json-generator.com/templates/z0aMAeTBHKq3/data”
token is “2oof0bzpgllll2a8k2fmmh6aaykeu8yptztdzke7”
but when I display the data in an Activity
, it just displays blank and exits the app. But When I change the URL without an auth/bearer token I can see the displayed data in the URL.
So my question: is how do I put the token in header so that I can have access and see the data displayed in the mobile phone?
I know it is simple but i am having a hard time.
This is my MainActivity.java
public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private RequestQueue requestQueue; private List<Movie> movieList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.recyclerview); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(this)); requestQueue = VolleySingleton.getmInstance(this).getRequestQueue(); movieList = new ArrayList<>(); fetchMovies(); } private void fetchMovies() { //Url with auth String url = "https://api.json-generator.com/templates/z0aMAeTBHKq3/data"; //URL without auth //String url = "https://jsonplaceholder.typicode.com/posts"; JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, 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); String userId = jsonObject.getString("userId"); String id = jsonObject.getString("id"); String title = jsonObject.getString("title"); String body = jsonObject.getString("body"); Movie movie = new Movie(title , userId , id , body); movieList.add(movie); } catch (JSONException e) { e.printStackTrace(); } MovieAdapter adapter = new MovieAdapter(MainActivity.this , movieList); recyclerView.setAdapter(adapter); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show(); } }); HashMap headers = new HashMap(); String authValue = "Bearer " + "2oof0bzpgllll2a8k2fmmh6aaykeu8yptztdzke7"; headers.put("Authorization", authValue); headers.put("Accept", "application/json; charset=UTF-8"); headers.put("Content-Type", "application/json; charset=UTF-8"); requestQueue.add(jsonArrayRequest); } }
this is my Movie.java
public class Movie { private String userId , id , title, body; public Movie(String userId , String id , String title , String body){ this.title = title; this.userId = userId; this.id = id; this.body = body; } public String getTitle() { return title; } public String getUserId() { return userId; } public String getId() { return id; } public String getBody() { return body; } }
this is my MovieAdapter.java
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieHolder> { private Context context; private List<Movie> movieList; public MovieAdapter(Context context , List<Movie> movies){ this.context = context; movieList = movies; } @NonNull @Override public MovieHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item , parent , false); return new MovieHolder(view); } @Override public void onBindViewHolder(@NonNull MovieHolder holder, int position) { Movie movie = movieList.get(position); holder.userId.setText(movie.getUserId().toString()); holder.id.setText(movie.getId()); holder.title.setText(movie.getTitle()); holder.body.setText(movie.getBody()); holder.constraintLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(context , DetailActivity.class); Bundle bundle = new Bundle(); bundle.putString("userId" , movie.getUserId()); bundle.putString("id" , movie.getId()); bundle.putString("title" , movie.getTitle()); bundle.putString("body" , movie.getBody()); intent.putExtras(bundle); context.startActivity(intent); } }); } @Override public int getItemCount() { return movieList.size(); } public class MovieHolder extends RecyclerView.ViewHolder{ TextView title , userId , id, body; ConstraintLayout constraintLayout; public MovieHolder(@NonNull View itemView) { super(itemView); userId = itemView.findViewById(R.id.userid_tv); title = itemView.findViewById(R.id.title_tv); id = itemView.findViewById(R.id.id_tv); body = itemView.findViewById(R.id.id_tv); constraintLayout = itemView.findViewById(R.id.main_layout); } } }
This is my VolleySingleton.java
public class VolleySingleton { private RequestQueue requestQueue; private static VolleySingleton mInstance; private VolleySingleton(Context context){ requestQueue = Volley.newRequestQueue(context.getApplicationContext()); } public static synchronized VolleySingleton getmInstance(Context context){ if (mInstance == null){ mInstance = new VolleySingleton(context); } return mInstance; } public RequestQueue getRequestQueue(){return requestQueue;} }
Advertisement
Answer
Try This
Change
private void fetchMovies() { //Url with auth String url = "https://api.json-generator.com/templates/z0aMAeTBHKq3/data"; //URL without auth //String url = "https://jsonplaceholder.typicode.com/posts"; JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, 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); String userId = jsonObject.getString("userId"); String id = jsonObject.getString("id"); String title = jsonObject.getString("title"); String body = jsonObject.getString("body"); Movie movie = new Movie(title , userId , id , body); movieList.add(movie); } catch (JSONException e) { e.printStackTrace(); } MovieAdapter adapter = new MovieAdapter(MainActivity.this , movieList); recyclerView.setAdapter(adapter); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show(); } }); HashMap headers = new HashMap(); String authValue = "Bearer " + "2oof0bzpgllll2a8k2fmmh6aaykeu8yptztdzke7"; headers.put("Authorization", authValue); headers.put("Accept", "application/json; charset=UTF-8"); headers.put("Content-Type", "application/json; charset=UTF-8"); requestQueue.add(jsonArrayRequest); }
To
private void fetchMovies() { // Url with auth String url = "https://api.json-generator.com/templates/z0aMAeTBHKq3/data"; // URL without auth // String url = "https://jsonplaceholder.typicode.com/posts"; JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, 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); String userId = jsonObject.getString("userId"); String id = jsonObject.getString("id"); String title = jsonObject.getString("title"); String body = jsonObject.getString("body"); Movie movie = new Movie(title , userId , id , body); movieList.add(movie); } catch (JSONException e) { e.printStackTrace(); } MovieAdapter adapter = new MovieAdapter(MainActivity.this , movieList); recyclerView.setAdapter(adapter); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show(); } }){ @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> headers = new HashMap<String, String>(); String authValue = "Bearer " + "2oof0bzpgllll2a8k2fmmh6aaykeu8yptztdzke7"; headers.put("Authorization", authValue); headers.put("Accept", "application/json; charset=UTF-8"); headers.put("Content-Type", "application/json; charset=UTF-8"); return headers; } }; requestQueue.add(jsonArrayRequest); }
put your headers code inside getHeaders() @Overtide
method