Data binding issue in Nested Recyclerview
Only 0th position of data are retrieved from API from JSON ARRAY. I mean right now only movies id=1,movies name=Brother and image id ="http://........../uploads/movie_thums_image/1555926668-slider-1.jpg" is retrieved. I have to implemented it in Nested Recyclerview , same as Play store. only data binding issue occurs for rows
"Category_name": "Hindi Movies",
"Category_id": "1",
"movies_id": [
"1",
"2"
],
"movies_name": [
"Brother",
"Hena"
],
"image": [
"http://........../uploads/movie_thums_image/1555926668-slider-1.jpg",
"http://........./uploads/movie_thums_image/1555928645-slider-3.jpg"
]
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String Category_name = jsonObject1.getString("Category_name");
Log.e("UUUUUUUUUU", jsonArray.length() + "");
String Category_id = jsonObject1.getString("Category_id");
JSONArray jsonArray1 = jsonObject1.getJSONArray("movies_id");
JSONArray jsonArray2 = jsonObject1.getJSONArray("movies_name");
JSONArray jsonArray3 = jsonObject1.getJSONArray("image");
for (int z = 0; z < jsonArray1.length(); z++) {
jsonArray1.get(z);
jsonArray2.get(z);
jsonArray3.get(z);
movieID = jsonArray1.getString(z);
movieName = jsonArray2.getString(z);
movieImage = jsonArray3.getString(z);
}
List<HorizontalMovieDataModel> list = new ArrayList<>();
VerticalMovieDataModel verticalMovieDataModel = new VerticalMovieDataModel();
verticalMovieDataModel.setCategory_name(Category_name);
verticalMovieAdapter = new VerticalMovieAdapter(veticalList, getActivity());
Log.e("VERTICAL", veticalList.size() + "");
rv_movies.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
rv_movies.setAdapter(verticalMovieAdapter);
// verticalMovieDataModel.setCategory_name(Category_id);
ArrayList<HorizontalMovieDataModel> singleItem = new ArrayList<HorizontalMovieDataModel>();
singleItem.add(new HorizontalMovieDataModel(movieName, movieID, movieImage));
verticalMovieDataModel.setHorizontalMovieDataModels(singleItem);
veticalList.add(verticalMovieDataModel);
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("data");
ArrayList<HorizontalMovieDataModel> singleItem = new ArrayList<HorizontalMovieDataModel>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String Category_name = jsonObject1.getString("Category_name");
Log.e("UUUUUUUUUU", jsonArray.length() + "");
String Category_id = jsonObject1.getString("Category_id");
JSONArray jsonArray1 = jsonObject1.getJSONArray("movies_id");
JSONArray jsonArray2 = jsonObject1.getJSONArray("movies_name");
JSONArray jsonArray3 = jsonObject1.getJSONArray("image");
singleItem.clear();
for (int z = 0; z < jsonArray1.length(); z++) {
movieID = jsonArray1.getString(z);
movieName = jsonArray2.getString(z);
movieImage = jsonArray3.getString(z);
singleItem.add(new HorizontalMovieDataModel(movieName, movieID, movieImage));
}
veticalList.add(new VerticalMovieDataModel(Category_name, Category_id, singleItem));
}
verticalMovieAdapter = new VerticalMovieAdapter(veticalList, getActivity());
Log.e("VERTICAL", veticalList.size() + "");
rv_movies.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
rv_movies.setAdapter(verticalMovieAdapter);
Related
These are my JSON data:
[
{"haberBaslik":"ekrem kimdir?"},
{"haberBaslik":"doğa kimdir?"},
{"haberBaslik":"biz kimiz?"},
{"haberBaslik":"fatih naptı?"}
]
and this code only shows the latest data, but I need to show my all data
JSONArray jsonarray = new JSONArray(s);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String Haberbaslik = jsonobject.getString("haberBaslik");
tv1.setText(Haberbaslik);
}
JSONArray jsonarray = new JSONArray(s);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String Haberbaslik = jsonobject.getString("haberBaslik");
tv1.setText(tv1.getText()+ " "+Haberbaslik);
}
I don't know how to parse a JSON if start with jsonArray instead jsonObject.
There's the JSON code.
[
{
"id": 1,
"title":
{
"rendered": "Apple apologises and fixes security flaw"
}
},
{
"id": 2,
"title":
{
"rendered": "Trophy hunting removes good genes and raises extinction risk"
}
}
...
]
I don't know how to get the JSONArray length. such as:
for (int i = 0; i < JSONArray.length(); i++)
{
JSONObject JSONObject1 = JSONArray.getJSONObject(i);
int id = JSONObject1.getInt("id");
string title = JSONObject1.getString("rendered");
}
Any help will be greatly appreciated!
Assuming that your json value is in this string variable : json
String json = "your_json_data";
JSONArray jsonarray = new JSONArray(json);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonObject = jsonarray.getJSONObject(i);
int id = jsonObject.getInt("id");
JSONObject titleObject = jsonObject.getJSONObject("title");
String rendered = titleObject.getString("rendered");
}
Try below code
JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonObject= jsonarray.getJSONObject(i);
int id = jsonObject.getInt("id");
JSONObject jsonInner= jsonObject.getJSONObject("title");
string title = jsonInner.getString("rendered");
}
Please help me to parse the following JSON data given below:
{
posts: [
{
count: 1,
user_id: "1",
name: "Dave Greeneberg",
email: "daveneberg#example.com",
profile_photo: "http://phontest.lbch.com//users/user1.jpg",
contest_count: "3",
photo_count: 19,
win_count: "0",
photos: [
"images/contest/diwali1.jpg",
"images/contest/diwalc2.jpg",
"images/contest/145043cd811.png",
"images/contest/145043def03411.jpg",
"images/contest/14504ger11.jpg"
]
}
]
}
I tried the following code but values in arrayList_ph is null. I am confused about how to parse this JSON content.
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = arr.getJSONObject(0).getString("name");
user_email = arr.getJSONObject(0).getString("email");
user_profile = arr.getJSONObject(0).getString("profile_photo");
user_count = arr.getJSONObject(0).getString("count");
user_photo_count = arr.getJSONObject(0).getInt("photo_count");
contest_count = arr.getJSONObject(0).getString("contest_count");
win_count = arr.getJSONObject(0).getString("win_count");
JSONArray ph_arr= arr.getJSONObject(0).getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
arrayList_ph.add(ph_arr.getString(in));
}
}
Please help me to parse that field.
Please try this solution.
This solutions is worked for me.
JSONObject object = new JSONObject("");
JSONArray arr = object.optJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.optJSONObject(index);
user = object1.optString("name");
user_email = object1.optString("email");
user_profile = object1.optString("profile_photo");
user_count = object1.optString("count");
user_photo_count = object1.optInt("photo_count");
contest_count = object1.optString("contest_count");
win_count = object1.optString("win_count");
JSONArray ph_arr = object1.optJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.opt(in).toString();
arrayList_ph.add(str);
}
}
change your parsing code to this ,
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = object1.getString("name");
user_email = object1.getString("email");
user_profile = object1.getString("profile_photo");
user_count = object1.getString("count");
user_photo_count = object1.getInt("photo_count");
contest_count = object1.getString("contest_count");
win_count = object1.getString("win_count");
JSONArray ph_arr= object1.getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.get(in).toString();
arrayList_ph.add(str);
}
}
Try -
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts”);
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = object1.getString("name");
user_email = object1.getString("email");
user_profile = object1.getString("profile_photo");
user_count = object1.getInt("count");
user_photo_count = object1.getInt("photo_count");
contest_count = object1.getString("contest_count");
win_count = object1.getString("win_count");
JSONArray photos_arr= object1.getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.get(in).toString();
arrayList_ph.add(str);
}
}
Please cross check Following:
1) I think your json response is not properly formatted.
Validate from this: https://jsonformatter.curiousconcept.com/
Your json response should be :
{
"posts":[
{
"count":1,
"user_id":"1",
"name":"Dave Greeneberg",
"email":"daveneberg#example.com",
"profile_photo":"http://phontest.lbch.com//users/user1.jpg",
"contest_count":"3",
"photo_count":19,
"win_count":"0",
"photos":[
{
"image":"images/contest/diwali1.jpg"
},
{
"image":"images/contest/diwalc2.jpg"
},
{
"image":"images/contest/145043cd811.png"
},
{
"image":"images/contest/145043def03411.jpg"
},
{
"image":"images/contest/14504ger11.jpg"
}
]
}
]
}
2) If that still does not work...Try debugging and post your log please...
I have this JSON content from a WordPress Site
"posts": [
{
"id": 67986,
"type": "post",
"title": "Launching New eBooks",
"thumbnail_images": {
"full": {
"url": "http://www.marineinsight.com/wp-content/uploads/2015/04/boiler-featured.png",
"width": 700,
"height": 500
},
"medium": {
"url": "http://www.marineinsight.com/wp-content/uploads/2015/04/boiler-featured-300x214.png",
"width": 300,
"height": 214
},
}
}
I want to fetch the url from medium to display as image. After referring to some SO questions, I have made this code and tried to go into the loop. But somehow i get the entire thumbnail_images
JSONObject jsono = new JSONObject(data);
jarray = jsono.getJSONArray("posts");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
JSONArray bigImage = object.getJSONArray("thumbnail_images");
for (int j = 0; j < bigImage.length(); j++) {
JSONObject tiObj = bigImage.getJSONObject(j);
JSONArray tiMed = tiObj.getJSONArray("medium");
for (int k = 0; k < tiMed.length(); k++) {
JSONObject tiMedU = tiMed.getJSONObject(i);
String imageURL = tiMedU.getString("url");
}
}
actor = new Actors();
actor.setName(object.getString("title"));
actor.setDescription(object.getString("url"));
actor.setImage(imageURL);
actor.setDob(object.getString("content"));
actorsList.add(actor);
}
Not able to figure out whats wrong in the loops above. Any help wld be great. Thanks
Try to use this
JSONObject jsono = new JSONObject(data);
jarray = jsono.getJSONArray("posts");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
JSONObject bigImage = object.getJSONObject("thumbnail_images");
JSONObject tiMed = bigImage.getJSONObject("medium");
String imageURL = tiMed.getString("url");
}
}
actor = new Actors();
actor.setName(object.getString("title"));
actor.setDescription(object.getString("url"));
actor.setImage(imageURL);
actor.setDob(object.getString("content"));
actorsList.add(actor);
}
jsonarray.getJSONObject(i).
getJSONObject("thumbnail_images").
getJSONObject("medium").getString("url")
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("posts");
for (int i = 0; i < jsonArray.length(); i++){
//JSONObject jsonObject1 = jsonArray.getJSONObject(i).getJSONObject("thumbnail_images");
System.out.println("apk----------------"+jsonArray.getJSONObject(i).getJSONObject("thumbnail_images").getJSONObject("medium").getString("url"));
}
response = CustomHttpClient.executeHttpGet("http://10.0.0.4:8000/login/?format=json&name="+username.getText().toString()+"&password="+pwd.getText().toString());
JSONArray jArray=new JSONArray(response);//Json Array
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);//Json Array To Json Object
Jenter code hereSONObject jsonObj2 = json_data.getJSONObject("fields");
usertype = jsonObj2.getString("usertype");
email = jsonObj2.getString("email");
}
Toast.makeText(getBaseContext(),email, Toast.LENGTH_SHORT).show();
I have this data form this URL
http://api.clicky.com/api/stats/4?site_id=32020&sitekey=71bf7c1681e22468&type=visitors&output=json&json_callback=data%22
and how to get this in android,
my problem is how to access all this objects and array, help me
[
{
"type": "visitors",
"dates": [
{
"date": "2014-12-25",
"items": [
{ "value":"70" }
]
}
]
}
]
JSONArray jsonArray = new JSONArray("here is your json string ") ;
int count = jsonArray.length() ;
for (int i = 0; i < count; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i) ;
String type = jsonObject.optString("type") ;
JSONArray datesArray = jsonObject.optJSONArray("dates") ;
int datesCount = datesArray.length() ;
for(int j = 0; j< datesCount; j++){
JSONObject dateItem = datesArray.get(j) ;
String dateStr = dateItem.optString("date");
JSONArray itemsArray = dateItem.optJSONArray("items");
for(int f = 0 ; f < itemsArray.length(); f++){
JSONObject valueJson = itemsArray.get(f);
String value = valueJson.optString("value");
}
}
}
I think, you can use GSON library for parsing your JSON. Just create class for JSON and use this code:
new Gson().fromJson(json, MyGsonClass.class);