Load names from JSON to Expandable List Android - android

How to load the Name of places from JSON File to Expandable List?My json file is stored on the assets folder. Im new to android.Please help me,thanks. heres the link to my code below:
http://textuploader.com/kv72

There is a problem in your "prepareListData()" method. You need to work on it, you are missing few steps & did some wrong steps. Follow below links :
1] How to set data to Expandable List View?
2] how to make ExpandableListView with JSON array
I hope, these will be helpful for you.

You could use a json parser like for instance Jackson where you can do annotations in java objects. In that way you can define the structure of your classes in the same maner as the json structure and insert the objects you create into the list. For example an Json object called User:
public class User {
#JsonProperty("username")
public String mUsername;
#JsonProperty("email")
public String mEmail;
}
You can even define other classes so that you can parse all of your json like for instance:
public class SomeData {
#JsonProperty("id")
public String mId;
#JsonProperty("users")
public List<User> mUsers;
}
In this example the json will have a list of User objects and the parser inserts them into the variable mUsers.
Link to jackson github: https://github.com/FasterXML/jackson-annotations

Related

Using Klaxon JSON for Android how can I get the following JSON in to a ListView?

I'm looking on how to use Klaxon to get my JSON into an Array for a LIstView. This is different then Java for two reasons. It is Kotlin and Klaxon.
Below is a array of zips and info for towns in a state. I'm pulling it via Fuel and I can get it into Klaxon to read parts of the objects into text fields but I want to build a list where each object has its own row. Was wondering how to get from point A klaxon into the List point B.
Not sure how to go about this.
{"towns":[{"zip":"02052","City":"Medfield","State":"MA","lat":"42.178","Long":"-71.3059"},{"zip":"02054","City":"Millis","State":"MA","lat":"42.1677","Long":"-71.3601"},{"zip":"02081","City":"Walpole","State":"MA","lat":"42.1429","Long":"-71.2571"},{"zip":"02030","City":"Dover","State":"MA","lat":"42.2341","Long":"-71.2861"},{"zip":"02056","City":"Norfolk","State":"MA","lat":"42.1185","Long":"-71.3287"},{"zip":"02032","City":"East
Walpole","State":"MA","lat":"42.1548","Long":"-71.2164"},{"zip":"02062","City":"Norwood","State":"MA","lat":"42.1855","Long":"-71.2022"},{"zip":"02071","City":"South
Walpole","State":"MA","lat":"42.105","Long":"-71.2705"},{"zip":"01770","City":"Sherborn","State":"MA","lat":"42.233","Long":"-71.3814"}
You need to have a object that describes your format and use your library to convert it from JSON (dont know about Klaxon but for GSON would be as show in sample)
models in separated files
public class Model {
public String zip, City, State, lat, Long;
}
public class ModeList {
public Model[] towns;
}
Then where you need your values:
List<Model> listOfModel = new Gson().fromJson(yourJsonAsStringHere);
To display in a ListView you use your list of Model to get the number of elements and each element to display one value.
Klaxon is pretty straight-forward you can check the samples into the README from the github https://github.com/cbeust/klaxon
With kotlin you even dont need to build types early.

How to read JSON array and display image in Android

I am developing android listing images app where I got array from server side. But I have issue to read array or don't know how to get it.
Below array i got from server API call:
{"lists":["http:\/\/xyz.com\/projects\/photo\/birthday\/1.jpg","http:\/\/xyz.com\/projects\/photo\/birthday\/3.jpg","http:\/\/xyz.com\/projects\/photo\/birthday\/4.jpg"],"Status":"1"}
How to read it and display images in loop.
Thanks
If you are using gson library for parsing json respose then you could simply write a separate class
like this
public class ImageResponse{
public String Status;
public List<String> lists;
}
NOTE: when creating a class the object names of the class should match with the tag names from the server..
Then using gson
ImageResponse detail = (new Gson()).fromJson(
response.toString(), ImageResponse.class);
where response is your response from the server
now u can access the list like
List<String> images =details.lists;
Now you have all the images in the list form, you can load the images using Picasso[lets say in a listview]
Picasso.with(context).load(images[0]).into(imageView);
For using this you should add GSON library to your dependency in your app gradle
compile 'com.google.code.gson:gson:2.2.4'
There are many other method too, feel free to search and find out may be there will be more optimized code than this..

How to start an android project coders view

I've been searching for the past week on how to develop an android project, read some on android developers page and on other websites like here, but no text was complete.
i have this project - i'm a php developer not a java, but could understand a bit about java lately.
the thing is i want to develop an android app using my website, i did output a json type data from my website, and gonna use them on the android app, i did the async request on android and could read the data form the website but :
first question: how to parse the json data correctly and convert it to array on android, i did that through:
Iterator<String> itr = myObject.keys();
while (itr.hasNext()) {
...
i don't know if that's the correct way, when i try to convert my json object to array, it gives me type mismatch.
second and more importantly:
how can create a "Block" like facebook posts style, or twitter style blocks, you know - blocks of json data, is it a linearlayout ? what do i call it ? and how can i add it to the UI dynamically, cuz these blocks are pulled from the website json data. so they are arrays...of blocks..
i'm kinda confused still, i need a start point.
Thank you!
excellent tutorial for beginners for android development
http://thenewboston.org/list.php?cat=6
and for your first question - how to parse json data correctly,
you can try using gson to convert the json data into POJO
otherwise you'd have to do myObject.opt(key) to make sure it is there
First question: you should use a library to parse JSON, it's simpler that way. Try gson. You should create a class, which holds the parsed object, like:
public class Taxi implements Serializable {
private static final long serialVersionUID = 1L;
#SerializedName("idTaxi")
private Integer idTaxi;
#SerializedName("name")
private String name;
//getter, setters, constructor, etc
}
Then when you get the JSON object, you can parse it:
Gson gson = new Gson();
Reader reader = new InputStreamReader(SOURCE_STREAM);
Taxi[] response = gson.fromJson(reader, Taxi[].class);
Second question: i think a ListView would be good for you. You can read a full tutorial about it here

Jackson annotations tutorials?

Does somebody aware of some good jackson annotations tutorials? Especially, how do you parse json array using jackson annotations?
Suppose I have json like this:
{
...
"item1": "aaa",
"item2": "bbb",
"fl": [
{
"item3": "ccc",
"item4": "ddd"
}
]
}
How does one parse json including array using jackson annotations?
I assume you are asking about how to map JSON into Java objects (parsing typically refers to lower level activity of decoding JSON content stream into another representation, which may or may not be set of objects).
With Jackson (as well as many other libs, Genson, GSON etc), you have choice of either binding it into a POJO with matching structure, say:
public class Value {
public String item1; // or some other type one can bind from JSON String
public String item2; // can alternatively use getters/setters instead of public fields
public List<EntryType> f1;
}
public class EntryType {
public String item3;
public String item4;
}
or reading it as a Tree representation like so:
JsonNode rootNode = mapper.readTree(inputSource);
In latter case, you need to traverse the tree nodes to find your data.
As to annotations: you only need to use annotations if you need to change default properties of binding; like mapping between JSON Object fields and POJO property names.
For Jackson-specific configuration including some of annotations, see Jackson-databind github project: https://github.com/FasterXML/jackson-databind/
There are three ways to parse json using Jackson -
Using a streaming Parser. (http://www.studytrails.com/java/json/java-jackson-json-streaming.jsp)
Parsing Json to a tree model - http://www.studytrails.com/java/json/java-jackson-json-tree-parsing.jsp
Data binding with our without annotations - data binding, data binding with filters, Polymorphic data binding

How to parse json page using gson?

I have a large json page which contains url:http://akhilmadanan.tk/Akhil/database.php.
While i am parsing this page using normal json parsing method it shows "OutOfMemoryError". For this i heard about GSON. Please any body help me get how to read the datas from below page using GSON.
any tutorial?
Here'a good tutorial:
http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
You can also check out their official user guide:
https://sites.google.com/site/gson/gson-user-guide
Hope this helps :-)
Instead of returning all the data, why don't you break it into chunks? That would require less memory at processing time.
Thats assuming you have access to the database level/response.
You can definitely go to links provided by others which are helpful
For brief you can add GSON library in your lib folder.
and use like this.
Gson gson=new Gson();
To get object from json
Model model=gson.fromJson(json,Model.class);
To convert to json
String json=gson.toJson(model);
I run your code and there are 3010 items of object
[
{
"cust_no":"70105615002",
"cust_name":"akhil",
"address":"kajffjkhfhhkjsd",
"area":"58695",
"ranges":"4586",
"plot":"69896",
"multifactor":"85758",
"electricity_meterno":"7895",
"water_meterno":"69358",
"gas_metrno":"78956",
"traffic_code":"4587855",
"last_meter":"58695",
"previous_reading":"25638",
"date":"589687",
"current_usage":"789654",
"current_balance":"45876",
"last_recipt":"236584"
},....
Now make a model equivalent to above name like
#SerializedName("cust_no")
private Long custNo;
#SerializedName("cust_name")
private Long custName;
..........
remember to add one list of same class type like
#SerializedName("custname")
private List<Customer> customerList;
and generate getters and setters of that Customer class;
after this
parse your data like this
CustomerModel customerModel=gson.fromJson(json,Customer.class);
you get all your data in customerModel;
To access data just use list of that class.
List<Customer> customerList=customerModel.getCustomerList();
Log.v("APP_NAME",""+customerList.size());

Categories

Resources