Android - How to display online value chart bar using MPAndroidChart? - android

There's a way how to set a value chart bar obtained from JSON website like this below??
[
{
"nilai": "1",
"jumlah": "18"
},
{
"nilai": "2",
"jumlah": "2"
},
{
"nilai": "3",
"jumlah": "7"
},
{
"nilai": "4",
"jumlah": "6"
},
{
"nilai": "5",
"jumlah": "3"
}
]
that's nilai is constant and the jumlah may changed the value,
I've reference from here but it's offline data.
I've tried to find any tutorial on internet, but the clue is scrimpy,
I'm using this code to try but i'm stuck at how to change AddValuesToBARENTRY() to my jumlah and AddValuesToBarEntryLabels() to my nilai from here
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String jumlah = c.getString("jumlah");
String nilai = c.getString("nilai");
HashMap<String, String> contact = new HashMap<>();
contact.put("id", nilai);
contact.put("name", jumlah);
contactList.add(contact);

Related

Don't know how to get description from OpenWeatherMap api, only the place and temperature

I've been working on making a simple weather app that displays the temperature, a description of the weather and the location. I'm able to get the location but can't figure out how to get the description of the weather from OpenWeatherMap and it's been bugging me for a few hours now. This is the part of the code in which I have this information laid out:
#Override
protected void onPostExecute(String result){
super.onPostExecute(result);
try {
JSONObject jsonObject = new JSONObject(result);
JSONObject weatherDatas = new JSONObject(jsonObject.getString("main"));
double temperature = Double.parseDouble(weatherDatas.getString("temp"));
int tempIn = (int) (temperature*1.8-459.67);
String placeName = (String) jsonObject.get("name");
MainActivity.tempeartureTextView.setText("" + tempIn);
MainActivity.placeTextView.setText(placeName);
Log.i("it made it", "to end of DownloadTask");
//using http://samples.openweathermap.org/data/2.5/weather?zip=94040,us&appid=b1b15e88fa797225412429c1c50c122a1 with zip code
} catch (Exception e) {
e.printStackTrace();
}
}
But for the life of me I cannot figure out how to get the description.
The rest of the code is all here: MainActivity: http://pastebin.com/e2NtTSiP
DownloadTask: http://pastebin.com/ScEREz52
I followed a youtube tutorial on this so I don't quite understand everything but I kinda wanna just do it over and over again till I can get it right.
String weatherInfo = jsonObject.getString("weather");
Log.i("Weather content", weatherInfo);
JSONArray arr = new JSONArray(weatherInfo);
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonPart = arr.getJSONObject(i);
String main = "";
String description = "";
main = jsonPart.getString("main");
description = jsonPart.getString("description");
MainActivity.descriptionTextView.setText(description);
}
This fixed it for me!
if your JSON response is following :
{
"coord": {
"lon": -122.08,
"lat": 37.39
},
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10n"
}
],
"base": "stations",
"main": {
"temp": 277.14,
"pressure": 1025,
"humidity": 86,
"temp_min": 275.15,
"temp_max": 279.15
},
"visibility": 16093,
"wind": {
"speed": 1.67,
"deg": 53.0005
},
"clouds": {
"all": 1
},
"dt": 1485788160,
"sys": {
"type": 1,
"id": 471,
"message": 0.0116,
"country": "US",
"sunrise": 1485789140,
"sunset": 1485826300
},
"id": 5375480,
"name": "Mountain View",
"cod": 200
}
In order to get weather description from the response,
get "weather" array
for each JSON object present inside weather array, get the "description" property of the JSON object
JSONObject obj = new JSONObject(response);
JSONArray arr = obj.getJSONArray("weather");
for (int i = 0; i < arr.length(); i++) {
JSONObject each = arr.get(i);
each.getString("description");
}

Can't access some member of JSON array Android

I have JSON array like this
[
{
"ride_id": "48",
"user_id": "22",
"event_id": "42",
"driver_id": "0",
"pick_up_location": "11111"
},
{
"ride_id": "48",
"user_id": "21",
"event_id": "42",
"driver_id": "0",
"pick_up_location": "11111"
},
{
"name": "ofir rosner",
"image": "",
"address": "Yokne\\'am Illit, Israel"
},
{
"name": "hhhh",
"image": "profilePictureOfUserId22.JPG",
"address": "ffffd"
}
]
im using OKhttp3 to retrieve my data like
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for (int i=0; i<array.length(); i++){
JSONObject object = array.getJSONObject(i);
...........
but the object of JSONObeject in place i only contains
{"ride_id":"50","user_id":"2","event_id":"44","driver_id":"0","pick_up_location":"11111"}
but I want to be able to do something like this object.setString("name")
or to get to the object in the place i for name, image and address
Your all object don't have name key-value pair so you can use optString , It returns an empty string if there is no such key
JSONArray array = new JSONArray(response.body().string());
for (int i=0; i<array.length(); i++){
JSONObject object = array.getJSONObject(i);
String name = object.optString("name");
// optionally use object.optString("name","No Name Found");
// To display some default string as name
if(!name.isEmpty()){
// we got the data , use it
Log.i("name is ",name); // display data in logs
}
}
or you can use has which Determine if the JSONObject contains a specific key.
JSONArray array = new JSONArray(response.body().string());
for (int i=0; i<array.length(); i++){
JSONObject object = array.getJSONObject(i);
String name;
if(object.has("name")){
name = object.optString("name"); // got the data , use it
}
}
{ // this object has no "name" key-value pair and so does some other object
"ride_id": "48",
"user_id": "22",
"event_id": "42",
"driver_id": "0",
"pick_up_location": "11111"
}

sort json response on click and display accordingly in titanium

Hi I have json response which looks like this.
{
"id": "7",
"issue_title": "Apr - May 2015",
"issue_no": "Issue 1.4",
"cover_image_url": "http://www.link.org/apr--may-2015-7.jpg",
"synopsis_pdf_url": "",
"advertisers_pdf_url": "",
"issue_date": "01-04-2015",
"issue_year": "2015"
},
{
"id": "3",
"issue_title": "Feb-Mar 2015",
"issue_no": "Issue 1.3",
"cover_image_url": "http://www.link.org/febmar-2015-3.jpg",
"synopsis_pdf_url": "http://www.link.org/febmar-2015-3.pdf",
"advertisers_pdf_url": "http://www.link.org/febmar-2015-3.pdf",
"issue_date": "01-02-2015",
"issue_year": "2015"
},
{
"id": "2",
"issue_title": "Dec 2014 - Jan 2015",
"issue_no": "Issue 1.2",
"cover_image_url": "http://www.link.org/dec-2014--jan-2015-2.jpg",
"synopsis_pdf_url": "",
"advertisers_pdf_url": "",
"issue_date": "01-12-2014",
"issue_year": "2014"
},
{
"id": "1",
"issue_title": "Oct - Nov 2014",
"issue_no": "Issue 1.1",
"cover_image_url": "http://www.link.org/oct--nov-2014-1.jpg",
"synopsis_pdf_url": "",
"advertisers_pdf_url": "",
"issue_date": "01-10-2014",
"issue_year": "2014"
}
Then I retrieved "issue_year" of each element and displayed in picker with multiple occurrence deleted.
Basically when the window loads all the elements are displayed but after that on a click of picker element (i.e 2014 ,2015 ) the elements should get display.
Tableview is used for displaying elements so on each click the array passed to tableview should get change according to year selected from picker.
var i,
len = singleData.length,
sorted = [],
obj = {};
for ( i = 0; i < len; i++) {
obj[singleData[i]] = 0;
}
for (i in obj) {
sorted.push(i);
}
for (var i = 0; i < sorted.length; i++) {
data[i] = Ti.UI.createPickerRow({
title : sorted[i]
});
}
$.picker.add(data);
$.picker.addEventListener('change',function(e){
//what will be the code here
});
Can anyone help on this?
This is fairly simple. here's what you need to do:
Add a custom property to your picker, containing the obj to be sent:
for (var i = 0; i < sorted.length; i++) {
data[i] = Ti.UI.createPickerRow({
title : sorted[i],
obj: MY_OBJ // HERE YOU NEED TO PASS THE OBJECT THAT WILL BE SENT TO DISPLAY
});
}
Next, in your listener, you can access that object using:
$.picker.addEventListener('change',function(e){
alert(e.source.obj); // This will alert the correct object you created in the picker!
});
Hope it helps!
-Lucas

How to retrieve data using json parsing when there are multiple inner nodes?

I am trying to retrieve data using a URL using Json Parsing in Android.
I have done the following coding but I am not able to figure out how to parse the node which is present in the inner node items.
My json array and codes are posted as below. Please guide me step by step.
Part of the Json Array
[
{
"items": [
{
"id": "11",
"Item_Id": "123",
"Item_Name": "Chicken Cream Soup",
"Price": "8",
"Currency": "AED",
"Category": "Soup",
"Description": "Creamy Chicken Soup with garnish & side helpings",
"Unit": "2",
"food_type": "Non",
"Image_Large": "/images_large/chickensoup.jpg",
"Image_Thumb": "/images_large/chickensoup.jpg",
"Timestamp": "6/23/2014 9:49:43 PM",
"Promotion": "",
"Item_Name_arabic": "حساء الطماطم",
"Item_Name_russian": "",
"Currency_arabic": "درهم",
"Currency_russian": "",
"Description_arabic": "حساء الطماطم",
"Description_russian": "",
"Note": "",
"Nutritional_info": "",
"extrafield_1": "",
"extrafield_2": "",
"preferncess": [
"No Salt",
"Extra Sugar"
],
"preferncess_ids": [
"1",
"2"
],
"price": [
"4",
"5"
],
"preferncess_arabic": [
"لا الملح",
"سكر اضافية"
]
},
MainActivity.class
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
items = jsonObj.getJSONArray(TAG_CONTACTS);
Log.i("json node",""+items);
// looping through All Contacts
for (int i = 0; i < items.length(); i++) {
JSONObject c = items.getJSONObject(i);
String id = c.getString("id");
//String name = c.getString(TAG_NAME);
Log.w("myid", id);
} catch (JSONException e) {
e.printStackTrace();
}
// fetch item array
JSONArray itemObjectArray = jsonStr.getJSONArray("items");
for (int i = 0; i < itemObjectArray.length(); i++) {
// fetch item object at i position
JSONObject itemObject = itemObjectArray.getJSONObject(i);
// fetch values from itemObject
String idValue = itemObject.getString("id");
String noteValue = itemObject.getString("Note");
// fetch the array
JSONArrayprefObject = itemObject.getJSONArray("preferncess");
// iterate throgh the array like
for (int i = 0; i < prefObject .length(); i++) {
JSONObject p = prefObject .getJSONObject(i);
}
}
preference is json array you can retrive it by c.getJSONArray("preferncess"); and then iterating. or in more generic you can Can you try something like following
if (c.get(key) instanceof String)
{
JSONObject Lawset = c.getString(key);
} else if (c.get(key) instanceof JSONObject)
{
JSONObject Lawset = c.getJSONObject(key);
//try to retrieve data from json object
}
else if (c.get(key) instanceof JSONArray)
{
JSONArray Lawset = c.getJSONArray(key);
//iterate to get data
}

JSON parser for single Item in Android

My requirement is simple, i have a JSON file in webserver and i have to parse it in android and i know the parsing method, but i have to display the output by different way in the following JSON file,
{
"Productcategory": [
{
"shop_cat_id": "1",
"shop_cat_name": "kurtis",
"shop_scat_id1": "1",
"shop_scat_id1name": "cotton kurtis",
"shop_scat_id2": "2",
"shop_scat_id2name": "Cotton Designer Kurtis",
"shop_scat_id3": "3",
"shop_scat_id3name": "soft Designer kurtis"
},
{
"shop_cat_id": "2",
"shop_cat_name": "Sarees",
"shop_scat_id1": "1",
"shop_scat_id1name": "Saree 1",
"shop_scat_id2": "2",
"shop_scat_id2name": "Saree 2",
"shop_scat_id3": "3",
"shop_scat_id3name": "Saree 3",
"shop_scat_id4": "4",
"shop_scat_id3name": "Saree 4"
},
{
"shop_cat_id": "3",
"shop_cat_name": "Anarkkali suits",
"shop_scat_id1": "1",
"shop_scat_id1name": "Readymade",
"shop_scat_id2": "2",
"shop_scat_id2name": "Stitched",
"shop_scat_id3": "3",
"shop_scat_id3name": "Unstitched"
},
{
"shop_cat_id": "4",
"shop_cat_name": "CottonLeggins",
"shop_scat_id1": "1",
"shop_scat_id1name": "LSize",
"shop_scat_id2": "2",
"shop_scat_id2name": "XLsize",
"shop_scat_id3": "3",
"shop_scat_id3name": "3XLsize",
"shop_scat_id4": "4"
},
{
"shop_cat_id": "5",
"shop_cat_name": "PattialaPantsset",
"shop_scat_id1": "1",
"shop_scat_id1name": "LSize",
"shop_scat_id2": "2",
"shop_scat_id2name": "XLsize",
"shop_scat_id3": "3",
"shop_scat_id3name": "3XLsize",
"shop_scat_id4": "4"
},
{
"shop_cat_id": "6",
"shop_cat_name": "Kids'AnarkkaliSuits",
"shop_scat_id1": "1",
"shop_scat_id1name": "LSize",
"shop_scat_id2": "2",
"shop_scat_id2name": "XLsize",
"shop_scat_id3": "3",
"shop_scat_id3name": "3XLsize",
"shop_scat_id4": "4"
}
]
}
I fetched the "shop_cat_name" field in a separate ListView and it looks like below,
I need to display the subcategory in separate ListView for each Category Clicked.
But when i parse the "shop_scat_id1name" field, the data fetching in all the array fields with different category name like below image,
My required output should be like below when i click the first category and similarly for other category based on their subcategory..
Category name i click
Kurtis
Subcategory should be
Cotton kurtis
Cotton Designer Kurtis
Soft Designer Kurtis
By what way i have to modify the JSON structure or anything i need to do in coding part?
Help Needed..
I guess you get the vaule from JSON like this : String name=JSON.getString("shop_scat_id1name");
It's wrong.
Wait a minute,I'll give you some examples.
My English is very poor.Very sorry.
First
you should create a entity like this that will stored value.
public class Productcategory implements Serializable{
/**
*
*/
private static final long serialVersionUID = 7249744975503046565L;
private String shop_cat_id;
private String shop_cat_name;
private String shop_scat_id1;
private String shop_scat_id1name;
private String shop_scat_id2;
private String shop_scat_id2name;
private String shop_scat_id3;
private String shop_scat_id3name;
...Ignore get and set method
}
Second
you can use gson or use a lot of json.getString....
// this is your json,I replace it with ""
String jsondata="";
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray Jarray = parser.parse(jsondata).getAsJsonArray();
ArrayList<Productcategory> pc_list= new ArrayList<Productcategory>();
for (JsonElement obj : Jarray) {
Productcategory pc = gson.fromJson(obj, Productcategory.class);
pc_list.add(pc);
}
Third
Now,you get a list,you can use it for your ListView and if you want to click the item,
you should use pc_list.get(position) to get a Productcategory,so you can fill detail page.
I tried to let you know what I'm saying.But my English sucks.
You can use expandablelistview where your group will be the main category and onclick subcategory will be displayed.
To do this you need to do is create a object of product which contains shop_cat_id,shop_cat_name,... etc and then create a list of this.
Then pass this list to the BaseExpandableListAdapter, since the variable in the object maintain its position in getGroupView set the shop_cat_name to groupview and other three subcategory to getChildView.
you need change your json with following, as you mention in your comment that your sub Category is not fix then you must send one jsonArray as inner of all category. i write one for you:
"Productcategory": [
{
"shop_cat_id": "1",
"shop_cat_name": "kurtis",
"shop_scat_id1": "1",
"SubCategory": [
"cotton kurtis",
"Cotton Designer Kurtis",
"soft Designer kurtis"
]
},
........
// other jsonObject is same
you need handle id number in your code, because you have simple id number that start from 2. if have a problem with that say to me.
Edit
for catching value on SubCategory you must use following code:
JSONObject json = jsonarray.getJSONObject(i);
JSONArray SubCategoryArray = json.getJSONArray("SubCategory");
for (int i = 0 ; i < SubCategoryArray.length() ; i++)
{
map.put( i+1 , SubCategoryArray.getString(i));
// with this code you have title and id of your subCategory in map
// id start from 1 ( i + 1).
}

Categories

Resources