FloFlaFli
FloFlaFli

Reputation: 19

Retrieve a url from a Json API

I want to know how to get a URL in a json and open it in a setOnClickListener of a RecyclerView

My API Json

{
  {
    "link": "https://example.com"
  },
  {
    ...
  }
}

My URLModel

public class URLModel {
    String Link;
    public MonlixModel(String link) {
        Link = link;
    }

    public String getLink() {
        return Link;
    }

    public void setLink(String link) {
        Link = link;
    }
}

I think the problem comes from my Adapter but I don't know where and how to fix it

public class URLAdapter extends RecyclerView.Adapter<URLAdapter.URLHolder> {
    Context mContext;
    List<URLModel> urlModels;

    public URLAdapter(Context mContext, List<URLAdapter> urlModels) {
        this.mContext = mContext;
        this.urlModels= urlModels;
    }

    @NonNull
    @Override
    public URLHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_view, parent, false);
        return new URLHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull URLHolder holder, int position) {
        holder.recyclerUrl.setOnClickListener(view -> {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(urlModels.get(position).getLink()));
            mContext.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return urlModels.size();
    }

    public static class URLHolder extends RecyclerView.ViewHolder {
        private final RecyclerView recyclerUrl;

        public URLHolder(@NonNull View itemView) {
            super(itemView);
            recyclerUrl = itemView.findViewById(R.id.recycler_url);
        }
    }
}

And finally my Main.java

private void getData() {
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        @SuppressLint("NotifyDataSetChanged") JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, Apis.JSON_API, null, response -> {
            for(int i = 0; i <=response.length(); i++){
                try {
                    JSONObject jsonObject = response.getJSONObject(i);
                    URLModelList.add(new URLModel(
                            jsonObject.getString("link")
                    ));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            URLAdapter adapter = new URLAdapter(getApplicationContext(), URLModelList);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            Toast.makeText(Main.this, "Success", Toast.LENGTH_SHORT).show();
        }, error -> Toast.makeText(Main.this, error.getMessage(), Toast.LENGTH_SHORT).show());
        requestQueue.add(jsonArrayRequest);
    }

I use Volley, I don't know if it's the most adapted but I succeeded with that, I also tried to change in my URLModel the String by a Uri. But I noticed that the JSONObject didn't have a getUri or something like that

In my Logcat it tells me it's a null object reference

Any help will be much appreciated!

Upvotes: 0

Views: 74

Answers (1)

ChandraShekhara
ChandraShekhara

Reputation: 177

Please check your JSON format first,

{
 "key_name1": {
    "link": "https://example.com"
  },
 "key_name2":{
    ...
  }
}

make sure your inner object should have key_name as above example or make this JSONObject to JSONArray like this

[
  {
    "link": "https://example.com"
  },
  {
    
  }
]

Upvotes: 1

Related Questions