I am creating sectioned recyclerview list.using server response
one for header and second for child how to add it dynamically in my sectioned recyclerview but when i am getting output in recyclerview last record inserted two time with childlist.
there is two json arrays from server like this
"status": "1",
"msg": "Success",
"resident": {[
"wing_id": "4",
"resident_details": [
{
"first_name": "Suraj",
"last_name": "Jumbad",
"wing_id": "4",
},
{
"first_name": "Laxman ",
"last_name": "Kathar",
"wing_id": "4",
}
]
}
here is my code
try {
if (response.has("status") && response.getString("status").equals("1")) {
JSONArray residents = response.getJSONArray("resident");
for (int i = 0; i < residents.length(); i++) {
JSONObject jsonObject_residents_details = residents.getJSONObject(i);
String wing_name = jsonObject_residents_details.getString("wing_name");
String wing_id = jsonObject_residents_details.getString("wing_id");
sectionHeaderss.add(new SectionHeader(child, wing_name, i));
JSONArray resident_details = jsonObject_residents_details.getJSONArray("resident_details");
for (int j = 0; j < resident_details.length(); j++) {
JSONObject jsonObject = resident_details.getJSONObject(j);
String wing_ids = jsonObject.getString("wing_id");
String first_name = jsonObject.getString("first_name");
childList.add(new Child(first_name, wing_ids, wing_name));
//child add
//sectionHeaderss.add(new SectionHeader(childList, wing_name, i));
}
//main add
if (!childList.isEmpty()) {
for (int k = 0; k < childList.size(); k++) {
if (childList.get(k).getId().equals(wing_id)) {
sectionHeaderss.add(new SectionHeader(childList, wing_name, i));
}
}
} else {
sectionHeaderss.add(new SectionHeader(child, wing_name, i));
}
}
adapterRecycler = new AdapterSectionRecycler(GroupListActivity.this, sectionHeaderss);
rv_resident_list.setAdapter(adapterRecycler);
if (adapterRecycler != null) {
adapterRecycler.notifyDataSetChanged();
}
} else {
Toast.makeText(GroupListActivity.this, response.get("msg").toString(), Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(GroupListActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
Functions.pbVisiblity(true, pb_rv_resident_list);
}
Related
Im getting problems with parsing Json.
my json response.
{
"Persons": [
{
"id": 0,
"name": "William",
"image": "http://www.images-image2312321356.jpg",
"colors": [
"White",
"Red",
"Green"
]
},
{...}
Im trying this. but it causes a compile error
public void onResponse(JSONObject responseObject) {
try {
JSONArray rs = responseObject.getJSONArray("Persons");
for (int i = 0; i < rs.length(); i++) {
try {
final JSONObject c = rs.getJSONObject(i);
String name = c.getString("name");
items.add(name);
));
}
} catch (JSONException e) {
e.printStackTrace();
}
Any idea? Im using volley
public void onResponse(JSONObject responseObject) {
JSONArray rs = responseObject.getJSONArray("Persons");
if (rs != null || rs.length() > 0) {
for (int i = 0; i < rs.length(); i++) {
JSONObject obj = rs.getJSONObject(i);
String id = obj.getString("id");
String name = obj.getString("name");
String img = obj.getString("image");
JSONArray colors = obj.getJSONArray("colors");
for (int ii = 0; ii < colors.length(); ii++) {
String color = colors.getString(ii);
colors.add(color);
}
}
}
}
i want to parse userinfo Array and some string in it. and getuserlistdata array and some string in it. please help me how to parse this response.This is my respons.
{
"status": "true",
"data": {
"userinfo": [
{
"id": "77",
"firstname": "Test",
}
],
"getuserlistdata": [
{
"address": "Kasauli, Himachal Pradesh, India"
"hostImage": [
{
"id": "551",
"hostid": "122",
"images": "user_21t657.jpg",
"description": ""
},
{
"id": "3954",
"hostid": "122",
"images": "user_251541535.jpg",
"description": ""
},
],
]
}
}
Sir, This my code.
protected Void doInBackground(Void...arg0) {
// Creating service handler class instance
ServiceHandler2 sh = new ServiceHandler2();
String url_links = "http://192.168.0.65/hostandguest/android/viewprofile?uid=77";
Log.d("url_links: ", "> " + url_links);
String jsonStr = sh.makeServiceCall(url_links, ServiceHandler2.GET);
Log.v("Profile:", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject obj = new JSONObject(jsonStr);
status = obj.getString("status");
data = obj.getString("data");
if (status.equalsIgnoreCase("false")) {
} else {
jsonarr = obj.getJSONArray("userinfo");
for (int i = 0; i < jsonarr.length(); i++) {
JSONObject c = jsonarr.getJSONObject(i);
String fname = c.getString("firstname");
Datein_arr.add(fname);
String id = c.getString("id");
}
jsonarr2 = obj.getJSONArray("getuserlistdata");
for (int j = 0; j < jsonarr2.length(); j++) {
JSONObject jo = jsonarr2.getJSONObject(j);
address = jo.getString("address");
Log.v("address", "" + address);
Log.v("Profile:", "777777777777777777777777777777777777");
hostimgrr2 = obj.getJSONArray("hostImage");
if (hostimgrr2.length() == 0) {
ch_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/user_12707aaf22_original.jpg");
det_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/user_12707aaf22_original.jpg");
} else {
for (int k = 0; k < hostimgrr2.length(); k++) {
JSONObject js = hostimgrr2.getJSONObject(k);
img = js.getString("images");
Log.v("img", "" + img);
if (k == 0) {
ch_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/" + img);
}
ch_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/" + img);
}
}
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Try like below code to parse JSONArray.
try {
private ArrayList<UserInfo> userInfoList = new ArrayList<UserInfo>();
JSONObject jsonObject = new JSONObject(jsonData); // jsonData is your reponse string
JSONArray userinfo = jsonData.getJSONArray("userinfo");
for(int i = 0; i < userinfo.length(); i++) {
JSONObject jObject = userinfo.getJSONObject(i);
UserInfo userInfo = new UserInfo();
userInfo.setId( jObject.getString("id") );
userInfo.setFirstname( jObject.getString("firstname") );
userInfoList.add(userInfo);
}
} catch (Exception e) {
e.printStackTrace();
}
UserInfo class as follow.
public class UserInfo {
public String id = "";
public String firstname = "";
public String getID() {
return coursePrice;
}
public void setID(String id) {
this.id = id;
}
public String getFirstname() {
return coursePrice;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
}
First you have to validate your jsondata.
You can goto JSONLint to check whether your json is valid or not
{
"status": "true",
"data": {
"userinfo": [
{
"id": "77",
"firstname": "Test"
}
],
"getuserlistdata": [
{
"address": "Kasauli, Himachal Pradesh, India",
"hostImage": [
{
"id": "551",
"hostid": "122",
"images": "user_21t657.jpg",
"description": ""
},
{
"id": "3954",
"hostid": "122",
"images": "user_251541535.jpg",
"description": ""
}
]
}
]
}
}
And You are parsing a JSONObject as String.
ie, it should be obj.getJSONObject("data"); instead of obj.getString("data");
Also check for other parsing issues like this.
I've usually use Retrofit to do all parsing "dirty" work, but recently I've decided to parse json "by the hands". And I can't figure out how to parse nested arrays in array, there is my json:
[
{
"title":"Phone",
"subs":[
{
"id":157291,
"title":"Cell Phone Service"
},
{
"id":524624,
"title":"Landline Phone Service"
},
{
"id":157298,
"title":"Voice Over IP"
}
]
},
{
"title":"TV and Internet",
"subs":[
{
"id":157292,
"title":"Hardwire Internet"
},
{
"id":178472,
"title":"Television"
},
{
"id":524625,
"title":"Wireless Internet"
}
]
},
{
"title":"Entertainment",
"subs":[
{
"id":522695,
"title":"Music and Movies"
}
]
},
{
"title":"Bill Payment",
"subs":[
{
"id":179845,
"title":"Home Utilities"
}
]
},
{
"title":"Games and Social",
"subs":[
{
"id":157297,
"title":"Games",
"subs":[
{
"id":525000,
"title":"Category:Casual"
},
{
"id":525001,
"title":"Category:Online Games"
},
{
"id":525002,
"title":"Category:Action and Shooter Games"
},
{
"id":525003,
"title":"Category:RPG"
},
{
"id":525005,
"title":"Category:Strategy"
},
{
"id":525006,
"title":"Category:Adventure"
},
{
"id":525008,
"title":"Category:Simulators"
},
{
"id":525171,
"title":"Category:Portals and Services"
},
{
"id":525265,
"title":"Category:Game artefacts"
}
]
},
{
"id":524626,
"title":"Social Networks"
},
{
"id":522901,
"title":"Dating"
}
]
},
{
"title":"Finances",
"subs":[
{
"id":522747,
"title":"Loan Repayment"
}
]
},
{
"title":"Everyday Purchases",
"subs":[
{
"id":288993,
"title":"Beauty, Health, and Sports"
}
]
},
{
"title":"Travel",
"subs":[
{
"id":523297,
"title":"Travel Reservations"
},
{
"id":524634,
"title":"Travel Agencies"
}
]
},
{
"title":"Websites",
"subs":[
{
"id":160550,
"title":"Advertising"
},
{
"id":233554,
"title":"Domain Hosting"
}
]
},
{
"title":"And also:",
"subs":[
{
"id":179843,
"title":"Charity"
},
{
"id":524635,
"title":"Online auctions"
},
{
"id":522887,
"title":"Miscellaneous"
}
]
}
]
I've got the second-level nested array, using this piece of code, but how can I get the third-level array?
String response = RequestManager.makeRequest();
StringBuilder sbResponse = new StringBuilder();
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
JSONArray nestedArray = jsonObject.getJSONArray("subs");
for (int j = 0; j < nestedArray.length(); j++) {
}
}
Log.d(LOG_TAG, sbResponse.toString());
} catch (JSONException e) {
e.printStackTrace();
}
And, by the way, how should I store this data in the database - I'm using Realm and I've created Category and SubCategory models, do I need to create another subcategory model for saving data from the third-level array?
Categoty model:
public class Category extends RealmObject {
#PrimaryKey
private String title;
private SubCategory subCategory;
//Getters and setters
}
And SubCategory model:
public class SubCategory extends RealmObject {
#PrimaryKey
private int id;
private String title;
//Getters and setters
}
String response = RequestManager.makeRequest();
StringBuilder sbResponse = new StringBuilder();
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
Category c=new Category();
JSONObject jsonObject = jsonArray.getJSONObject(i);
c.setTitle(jsonObject.getString("title"));
JSONArray nestedArray = jsonObject.getJSONArray("subs");
for (int j = 0; j < nestedArray.length(); j++) {
SubCategory s=new SubCategory();
JSONObject nestedObject= nestedArray.getJSONObject(i);
s.setId(nestedObject.getString("id"));
s.setTitle(nestedObject.getString("title"));
}
c.setSubCategory(s);
}
Log.d(LOG_TAG, sbResponse.toString());
} catch (JSONException e) {
e.printStackTrace();
}
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
JSONArray nestedArray = jsonObject.getJSONArray("subs");
for (int j = 0; j < nestedArray.length(); j++) {
JSONObject nestObj = nestedArray.getJSONObject(j);
if(nestObj.has("subs"))
{
JSONArray insideArray = nestObj.getJSONArray("subs");
for (int k = 0; k < insideArray .length(); k++) {
}
}
}
}
Log.d(LOG_TAG, sbResponse.toString());
} catch (JSONException e) {
e.printStackTrace();
}
how can I fetch this response from URL in android?
1. //main array
[
//array in main array
[
//object in inner array
{
//data to be fetched here
"user_id": "8035",
"sr_no": "MG2459",
"user_type": "2",
"name": "Allen"
}
],
//2nd array in main array
[
{
"user_id": "8035",
"sr_no": "MG2459",
"user_type": "2",
"name": "TestName"
}
]
]
try below code :-
try {
JSONArray ja = new JSONArray(ur string);
for (int i = 0; i < ja.length(); i++)
{
JSONArray ja1 = ja.getJSONArray(i);
for (int j = 0; j < ja1.length(); j++) {
JSONObject jo = ja1.getJSONObject(j);
String user_id = jo.getString("user_id");
String sr_no = jo.getString("sr_no");
String user_type = jo.getString("user_type");
String name = jo.getString("name");
}
}
} catch (Exception e) {
// TODO: handle exception
}
I want to get only some selected values from the JSON response.For example if JSON contains 100 string values then i need to take the values which are starting with # symbol from that 100 values.
How can I do that ?
Following is my JSON,
[{"Obj" :
{ "ID":"11",
"NAME":"XYZ",
"GENDER":"M"
}
{ "ID":"11",
"NAME":"#XYZ",
"GENDER":"M"
}
{ "ID":"11",
"NAME":"#XYZ",
"GENDER":"M"
}
}]
Here I need to fetch Name which having # symbol
You can use below method to get the Name which start with # :
JSONObject jsonObject = new JSONObject(response);// u can change it as per your need
JSONArray jArray = jsonObject.getJSONArray("Obj");// if your `Obj` is an JsonArray
for (int i = 0; i < jArray.length(); i++) {
String json_name = jArray.getJSONObject(i).getString("NAME");
if(json_name.startsWith("#"))
{
Log.d(TAG,"It start with #");
}
}
Try out as below:
private void parseJSON(String json) {
try {
JSONArray items = new JSONArray(<your Json Response>);
for (int i = 0; i < items.length(); i++) {
JSONObject item = items.getJSONObject(i);
System.err.println("Object---" + item.getString("Obj"));
JSONObject obj=item.getJSONObject("Obj");
for (int j = 0; j < obj.length(); j++) {
String Name=obj.getString("NAME");
if(obj.getString("NAME").toString().contains("#"))
{
Log.d("Name starts with-->", Name);
}
else
{
Log.d("Name does not start with-->", Name);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Try this.
first of all that's not a valid json may be below json is correct format
[
{
"Obj": [
{
"ID": "11",
"NAME": "XYZ",
"GENDER": "M"
},
{
"ID": "11",
"NAME": "#XYZ",
"GENDER": "M"
},
{
"ID": "11",
"NAME": "#XYZ",
"GENDER": "M"
}
]
}
]
if your response like above try below code this may help you.
try {
JSONArray jsoArray = new JSONArray(json);
JSONArray JobjArray = jsoArray.getJSONObject(0).getJSONArray("Obj");
for(int i=0; i < JobjArray.length(); i++)
{
JSONObject Jobj = JobjArray.getJSONObject(i);
Iterator<String> iter = Jobj.keys();
while (iter.hasNext()) {
String key = iter.next();
Log.v("key--", key);
try {
Object value = Jobj.get(key);
Log.v("value--", ""+value);
String str_value = value.toString().trim();
if(str_value.startsWith("#"))
{
Log.d(""+str_value,"value started with #");
}
} catch (JSONException e) {
// Something went wrong!
e.printStackTrace();
}
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}