How to draw android spinner option from JSON [duplicate] - android

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 5 years ago.
I am new to android development. I am stuck try to show dropdown's data into android . Data will come from server as a json format. I am using volley library for HTTP requests. Here is my volley response
{"blood_group_map":[{"value":"1","text":"O+"},{"value":"2","text":"O-"},{"value":"3","text":"A+"},{"value":"4","text":"A-"},{"value":"5","text":"B+"},{"value":"6","text":"B-"},{"value":"7","text":"AB+"},{"value":"8","text":"AB-"}]}
In this response I want draw blood_group_map key in Spinner option list like this HTML. Here value key is used for value and text key is used for display text.
<option value="1">O+</option>
Update:
I parsed this JSON but I want to send spiner value property to server and display text property. How can I send value to server not text ?
For Example if I select O+ blood group then I send spinner value 1 to server.

Use GSON parser to parse data to from json.
Basic working of GSON is: you build an object with structure same as your json.
All you have to do is pass it to spinner. An example of GSON parser here: GSON parser example
Spinner example: Spinner example

Related

get data inside of a url in android [duplicate]

This question already has answers here:
Simple parse JSON from URL on Android and display in listview
(6 answers)
parse simple json string in android
(4 answers)
Closed 7 years ago.
i have a url that inide of this url is json code .
i need this content to decode. for example if my url is : http://url.com
i have this json in it:
{ "employee":{"mesg":"username is exsist!","id":0,"name":0,"username":0,"email":0,"status":500} }
i need from url.com this data to put in a variable and decode this.
for decoding i dont have problem . just i need to get data from url , like file_get_contents in php .
, thanks
NOTICE:
i use HttpClient for this solution . but it is deprecated . is another way for this?
You can use IOUtils.toString(url) from Apache Commons library.

Display json string result in listview in android

How to display JSON string result in Android ListView
Here is the JSON string
[{"USER_ID":83,"PROJECT_BY_DETAILS":"An adaptation of a nursery rhyme into a dramatic film"},{"USER_ID":88,"PROJECT_BY_DETAILS":"Test - over ye mountain blue "}]
I need to display USER_ID and PROJECT_BY_DETAILS in a ListView
if you got this json from server - you can use retrofit for parsing information into object.
or you can use default android classes for parsing
http://developer.android.com/reference/org/json/package-summary.html
parse this json to arraylist of objects, and create listview adapter
Create a Model to save your json Data
Create an Adapter
Use Volley Library to do the request
Do the request using volley and save in your model
Notify your list that you have new data
A complete example of how to use volley with json in a list is here

Decode json response in android

I need to decode a json response sent from php in android.
JSON Response
{"VehicleNumber":"1234FB14","Make":"BMW","Model":"X5","Type":"Car","Color":"Black","EngineCapacity":"1800cc","Fuel":"Diesel","DateOfReg":"31-Dec-2013","OwnerNIC":"B1234567890123","serviceArray":[[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1800","Description":"tponggshmbch"}],[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1900","Description":"tponggshmbch"}]]}
The response consists of values for their respective keys and I can retrieve these value by just calling getString(key) in java. {key:value,2D Array[][]}
The problem is that I can't decode the 2D Array being passed here. I get it in a string format in java and if I display it in my android app I can view this:
[[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1800","Description":"tponggshmbch"}],[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1900","Description":"tponggshmbch"}]]
But i need to retrieve all the value from the 2D array.
Any help please.
If Your json is quite big I recommend using gson or Jackson libraries.

how to extract web text in android like rss to display it on android GUI?

how to extract some criteria from this page http://www.zigwheels.com/api/zigtvApi.php?method=data&module=News&section=News
and filter this ( content_id , thumbnail, summary , headline , image) to display them as rss feeds in my android GUI
The output from that URL looks like a JSON feed. You can easily parse JSON data in Android using the JsonObject - see this tutorial for a comprehensive example.
A better (and probably easier) solution would be to use Google Gson and extract the object that way. I've written a full (compilable) program you can use as an example here.
You are receiving a JSONArray from that page. You should create some objects from that JSONArray which will be later displayed in a ListView. For retrieving objects from that feed you could use Json or Gson as #Marvin Pinto suggested, or you could also have a look at my ObjectFactory project, which is a very simple and easy to use parser. It can simply create your objects from a json or xml feed, and you could also use it asynchronously.
After you fetch your objects, with your desired option, you can create a ListAdapter and use it to display your objects in your ListView.

How can i get data from web service using android

Hi, I am new to android. I want to get data from webservice. in which key's are store in an array . and ley values stores in another array. In webservice data are display like,
{"1":{"para_id":"1","para_parent_id":"0","para_level":"0","para_type":"0","para_value":"Salution","para_desc":"Salution","para_sort_order":"1","para_tech_desc":"Salution","created_dt":"2011-08-11 19:47:05","created_by":"1","updated_dt":"2011-08-11 19:47:05","updated_by":"1","status":"A"},"1001":{"para_id":"1001","para_parent_id":"1","para_level":"0","para_type":"1","para_value":"Mr.","para_desc":"Mr.","para_sort_order":"1","para_tech_desc":"Mr.","created_dt":"2011-08-11 19:47:21","created_by":"1","updated_dt":"2011-08-11 19:47:21","updated_by":"1","status":"A"},"1002":{"para_id":"1002","para_parent_id":"1","para_level":"0","para_type":"1","para_value":"Ms.","para_desc":"Ms.","para_sort_order":"1","para_tech_desc":"Ms.","created_dt":"2011-08-11 19:49:00","created_by":"1","updated_dt":"2011-08-11 19:49:00","updated_by":"1","status":"A"},"1003":{"para_id":"1003","para_parent_id":"1","para_level":"0","para_type":"1","para_value":"Mrs.","para_desc":"Mrs.","para_sort_order":"1","para_tech_desc":"Mrs.","created_dt":"2011-08-12 09:40:14","created_by":"1","updated_dt":"2011-08-12 09:40:14","updated_by":"1","status":"A"},"2":{"para_id":"2","para_parent_id":"0","para_level":"0","para_type":"0","para_value":"Appointment Types","para_desc":"Appointment Types","para_sort_order":"1","para_tech_desc":"Appointment Types","created_dt":"2011-08-16 15:43:15","created_by":"1","updated_dt":"2011-08-16 15:43:15","updated_by":"1","status":"A"},"2001":{"para_id":"2001","para_parent_id":"2","para_level":"0","para_type":"2","para_value":"Scheduled","para_desc":"Scheduled","para_sort_order":"1","para_tech_desc":"Scheduled","created_dt":"2011-08-16 15:43:36","created_by":"1","updated_dt":"2011-08-16 15:43:36","updated_by":"1","status":"A"},"2002":{"para_id":"2002","para_parent_id":"2","para_level":"0","para_type":"2","para_value":"Cancelled","para_desc":"Cancelled","para_sort_order":"1","para_tech_desc":"Cancelled","created_dt":"2011-08-16 15:43:51","created_by":"1","updated_dt":"2011-08-16 15:43:51","updated_by":"1","status":"A"},"2003":{"para_id":"2003","para_parent_id":"2","para_level":"0","para_type":"2","para_value":"Re-Scheduled","para_desc":"Re-Scheduled","para_sort_order":"1","para_tech_desc":"Re-Scheduled","created_dt":"2011-08-16 15:44:24","created_by":"1","updated_dt":"2011-08-16 15:44:24","updated_by":"1","status":"A"},"2004":{"para_id":"2004","para_parent_id":"2","para_level":"0","para_type":"2","para_value":"Deleted","para_desc":"Deleted","para_sort_order":"1","para_tech_desc":"Deleted","created_dt":"2011-08-16 15:44:38","created_by":"1","updated_dt":"2011-08-16 15:44:38","updated_by":"1","status":"A"},"3":{"para_id":"3","para_parent_id":"0","para_level":"0","para_type":"0","para_value":"Customer Status","para_desc":"Customer Status","para_sort_order":"1","para_tech_desc":"Customer Status","created_dt":"2011-08-29 12:51:48","created_by":"1","updated_dt":"2011-08-29 12:51:48","updated_by":"1","status":"A"},"3001":
I want to sore key "1" ,"2", "3" in an array and related value in another array.and get the data. How can i do this?
Well, this looks like JSON, so use a JSON-Parser, for a tutorial see here. You can make your JSON more readable (especially when asking on SO) using the JSON beautifier

Categories

Resources