[英]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我想知道如何在 json 中获取 URL 并在 RecyclerView 的 setOnClickListener 中打开它
My API Json我的 API Json
{
{
"link": "https://example.com"
},
{
...
}
}
My URLModel我的 URL 模型
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最后是我的 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
.我使用 Volley,我不知道它是否最适合但我成功了,我还尝试通过
Uri
在我的URLModel
中更改String
。 But I noticed that the JSONObject
didn't have a getUri
or something like that但我注意到
JSONObject
没有getUri
或类似的东西
In my Logcat it tells me it's a null object reference
在我的 Logcat 中,它告诉我这是一个
null object reference
Any help will be much appreciated!任何帮助都感激不尽!
Please check your JSON format first,请先检查您的 JSON 格式,
{
"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确保您的内部对象应具有上述示例的 key_name 或将此 JSONObject 设置为 JSONArray 像这样
[
{
"link": "https://example.com"
},
{
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.