i my app in MainActivity View a execute a task that give me a json result.
I would like to know if is possibile to add dynamically a fab button into layout of other view in the number of json result.
Example if my json result have 3 items add 3 fab button to other view layout
This is my code about json call:
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("businesses");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(0);
//String id = c.getString("id");
String name = c.getString("name");
String phone = c.getString("phone");
Taxi_Number = "tel:"+phone;
}
There is some example to do this or is not possible?
Thanks
Related
I am fetching data from JSON to android.But, I am getting an empty JSON response. The PHP code which generates JSON data is as follows:
$result = $conn->query("SELECT dbname FROM users ORDER BY dbname ASC");
//defined second array for dbnames' list
$dblist = array();
while($row = $result->fetch_assoc()){
//array_push($response['dblist'],$row['dbname']);
$dblist[] = array('name'=>$row['dbname']);
}
$response['dblist'] = $dblist;
This is the JSON response.
{"dblist":[{"name":"a"},{"name":"arsod"}]}
The Java code to fetch data in android is as follows:
JSONObject obj = new JSONObject(s);
JSONArray names = obj.getJSONArray("dblist");
for(int i=0; i < names.length(); i++) {
JSONObject n = names.getJSONObject(i);
String name = n.getString("name");
Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
institutes.add(name);
}
where institutes is an ArrayList in which I want to add each fetched element. But while fetching the data, I get the error in logcat org.json.JSONException: End of input at character 0 of. What is going wrong?
Considering the following:
s = {"dblist":[{"name":"a"},{"name":"arsod"}]}
Your code should be like,
JSONObject obj = new JSONObject(s);
JSONArray names = obj.getJSONArray("dblist");
for(int i=0; i < names.length(); i++) {
JSONObject n = names.getJSONObject(i);
String name = n.getString("name");
Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
institutes.add(name);
}
remove this line:
JSONObject object = obj.getJSONObject("dblist");
and replace all occurences of object. with obj.
your JSONObject obj doesn't contain "dblist" object, there is only array inside it, so you should look for getJSONArray("dblist") straight inside obj
edit: you are parsing different String s, I've just checked this code:
final String s = "{\"dblist\":[{\"name\":\"a\"},{\"name\":\"arsod\"}]}";
JSONObject obj = new JSONObject(s);
JSONArray names = obj.getJSONArray("dblist");
for (int i = 0; i < names.length(); i++) {
JSONObject n = names.getJSONObject(i);
String name = n.getString("name");
Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
}
which is almost exacly same as your and works pretty fine...
I want to load JSON data into the List view with subtitle and images (Left side on the row).
Here below I have posted my sample JSON response code :
for (int i = 0; i < contacts.length(); i++) {
JSONObject obj = contacts.getJSONObject(i);
Iterator keys = obj.keys();
while(keys.hasNext()) {
// loop to get the dynamic key
String currentDynamicKey = (String)keys.next();
// store key in an arraylist which is A,B,...
// get the value of the dynamic key
JSONArray currentDynamicValue = obj.getJSONArray(currentDynamicKey);
Log.d("Response: ", "> " + currentDynamicKey);
int jsonrraySize = currentDynamicValue.length();
if(jsonrraySize > 0) {
for (int ii = 0; ii < jsonrraySize; ii++) {
JSONObject nameObj = currentDynamicValue.getJSONObject(ii);
String name = nameObj.getString("name");
System.out.print("Name = " + name);
//Log.d("Response: ", "> " + name);
//store name in an arraylist
}
}
}
}
I want to Show Tittle : currentDynamicKey values and Sub Title : Name string values.
If you already have your data and you don't necessarily need to integrate the server requests with your adapter, then you really just need an adapter which supports a JSONArray. There's a really nice open source one within the Advanced-Adapters library. Then it's just a matter of passing the JSONArray to the adapter and setting the adapter to a ListView.
My JSON code is as follows:
returnString = "";
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
JSONObject c = json_data.getJSONObject("data");
Log.i("log_tag","statues: "+c.getString("statues"));
returnString = c.getString("statues"); }
Now I want to see the values within my JSONArray. The value for statues is either 0 or 1. Further, I will use them in an if else phrase.
What I mean is something like this:
if(value of statues = 0){
do something}
else{
do something else}
How would I do that?
If you want only first status value in the JSONArray
to get the first value from JSONArray simply do outside of the loop and before the if statement:
JSONObject FirstArray = jArray.getJSONObject(0); // first Array in JSONArray
JSONObject data = FirstData.getString("data");
String FirstStatusValue = data.getString("Status");
then parse FirstStatusValue to int and use it in if.
I hope I understood your question well, if I didnt please correct me
if you want all status value within the JSONArray
before the for loop declare an array of string to and add all the status value in it then loop:
String[] StatusArray = new String[jArray.length()]; // will make the status array has the same length as the JSONArray
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
JSONObject c = json_data.getJSONObject("data");
Log.i("log_tag","statues: "+c.getString("statues"));
StatusArray[i] = c.getString("status");
}
then for if statement add for loop then check the statement like:
for (int i = 0; i < StatusArray.length(); i++)
{
if (StatusArray[i] == 0)
// do something
else
// do something
}
I have alot of link to get data from web hence I wanted to use loop to retrieve each URL's data but I had trouble in making JSObject as array.
JSONObject[] jsObjectallnewstype;
JSONArray[] jsonArrayallnewstype = null;
for(int i = 0; i < categories.length(); i++)
{
JSONObject c = categories.getJSONObject(i);
// Storing each json item in variable
String title = c.getString(TAG_TITLE);
String url = c.getString(TAG_URL);
jsObjectallnewstype[i] = JSONFunction.getnewstype(title, url); //java.lang.NullPointerException
jsonArrayallnewstype[i] = jsobjectallnewstype[i].getJSONArray(TAG_NEWLIST);
}
This line jsObjectallnewstype[i] get null error although the log shows JSONFunction.getnewstype successfully retrieve the data.
And i am also worry tat second line jsonArrayallnewstype[i] could cause same error as well.
So JSObject cant be put as array? If so what are the alternative??
To fix your current code, you need to initialize your array. This is why you are getting a NPE:
JSONObject[] jsObjectallnewstype = new JSONObject[categories.length()];
JSONArray[] jsonArrayallnewstype = new JSONArray[categories.length()];
for(int i = 0; i < categories.length(); i++)
{
JSONObject c = categories.getJSONObject(i);
// Storing each json item in variable
String title = c.getString(TAG_TITLE);
String url = c.getString(TAG_URL);
jsObjectallnewstype[i] = JSONFunction.getnewstype(title, url); //java.lang.NullPointerException
jsonArrayallnewstype[i] = jsobjectallnewstype[i].getJSONArray(TAG_NEWLIST);
}
In the below code, I can display the first returned JSON place name result from my Google Places URL. Rather than just the first item, I'd like to display the whole returned list of place names. What would be the easiest way of doing this? Can you point me to a tutorial, or give sample code?
HttpGet get = new HttpGet(bcURL.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if(status == 200){
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
json = new JSONObject(data);
JSONArray timeline = json.getJSONArray("results");
JSONObject lastport = timeline.getJSONObject(0);
return lastport;
}else{
Toast.makeText(NewGPS3.this, "oops", Toast.LENGTH_SHORT);
return null;
}
The line JSONObject lastport = timeline.getJSONObject(0); shows the first name. I'd like to show a list rather than just the first item.
You are going to need to use a JSONArray object in order to get at the array. It should go something like this:
JSONObject entireObject = new JSONObject("your JSON string here");
JSONObject mainObject = entireObject.getJSONObject("photos"); // main object in entire object
JSONArray arrayObject = mainObject.getJSONArray("photo"); // array object in the main object
You can then iterate through this array just like any other:
for (int i = 0; i < arrayObject.length(); i++) {
JSONObject photoData = arrayObject.getJSONObject(i);
String yourString = photoData.getString("id");
}
I got some of this out of Pro Android Media. There is a really good JSON api call example in this book. You can probably get the source code for free, too.
http://www.amazon.com/Pro-Android-Media-Developing-Smartphones/dp/1430232676/ref=sr_1_1?ie=UTF8&qid=1343062690&sr=8-1&keywords=android+media
for(int i = 0; i < timeline.size(); i++){
JSONObject lastport = timeline.getJSONObject(i);
//Save the item to a ArrayList and use that list for your ListAdapter.
googlePlaces.add(lastPort);
}