1

I'm using JSONObject to parse the JSON file and get its contents. Everything goes fine but tags aren't showing in the RecyclerView.

Here's the code :

private void direct_url(){
    v_title = findViewById(R.id.vid_title);
    String url = kw_url_holder.getText().toString();
    String server_tag_url = "https://server.com/json.json";
    StringRequest request = new StringRequest(Request.Method.GET, server_tag_url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                String title,views,likes,dislikes,publishedon,duration;
                JSONObject object=new JSONObject(response);
                title = object.getString("title");
                v_title.setText(title);
                JSONArray tagsJsonArray = object.getJSONArray("tags");
                for(int i=0; i<tagsJsonArray.length();i++){
                    try {
                        JSONObject tagObj = new JSONObject();
                        tagObj = tagsJsonArray.getJSONObject(i);
                        TagUrlResultsModel tagUrlResultsModel = new TagUrlResultsModel();
                        tagUrlResultsModel.setV_tags(tagObj.getString(String.valueOf(i)));
                        url_result.add(tagUrlResultsModel);
                    }catch (JSONException e){
                        e.printStackTrace();
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("error",error.toString());
        }
    });
    url_queue = Volley.newRequestQueue(tags.this);
    url_queue.add(request);
}

The JSON:

{
    "title": "The Title",
    "tags": ["tag1", "tag2"]
}

An error in the logs:

Error: java.lang.String cannot be converted to JSONObject

4
  • JSONObject object=new JSONObject(String.valueOf(response)); Commented Apr 26, 2020 at 14:56
  • at first show us whats response return Commented Apr 26, 2020 at 14:57
  • I didn't know what you mean by "response"? I' fetching the given JSON file from the server and trying to parse tags into RecyclerView Commented Apr 26, 2020 at 15:05
  • I've pasted the entire function that I'm using. [Question Updated] Commented Apr 26, 2020 at 15:07

1 Answer 1

1

The problem is inside your for loop in:

JSONObject tagObj = new JSONObject();
tagObj = tagsJsonArray.getJSONObject(i);
TagUrlResultsModel tagUrlResultsModel = new TagUrlResultsModel();
tagUrlResultsModel.setV_tags(tagObj.getString(String.valueOf(i)));
url_result.add(tagUrlResultsModel);

It should be

String tag;
tag = tagsJsonArray.getString(i);
TagUrlResultsModel tagUrlResultsModel = new TagUrlResultsModel();
tagUrlResultsModel.setV_tags(tag);
url_result.add(tagUrlResultsModel);

Using getString() instead of getJSONObject() as the content of that JSONArray is just strings.

That's why you are getting in that catch:

Error: java.lang.String cannot be converted to JSONObject

Sign up to request clarification or add additional context in comments.

6 Comments

I've done this but tags aren't displayed in the RecyclerView. String tag; tag = tagsJsonArray.getString(i); TagUrlResultsModel tagUrlResultsModel = new TagUrlResultsModel(); tagUrlResultsModel.setV_tags(String.valueOf(i)); url_result.add(tagUrlResultsModel);
It should be setV_tags(tag) with that change as tag contains the string itself, check the updated answer
Still not showing, it's unusual as it says No adapter attached; skipping layout. However, I've set the adapter
Then you are likely to have another bug on the RecyclerView adapter, which is not in the question
I've checked the Logs, all the tags are fetched by the code. Thanks a lot :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.