Related
I'm fetching data using API and the one of the data is a HTML formatted text.
The data fetched from the API
{
"id": 4632,
"title": "Soy-and-Ginger-Glazed Salmon with Udon Noodles",
"summary": "Soy-and-Ginger-Glazed Salmon with Udon Noodles might be a good recipe to expand your main course collection. This dairy free and pescatarian recipe serves 4 and costs <b>$5.91 per serving</b>. One serving contains <b>552 calories</b>, <b>48g of protein</b>, and <b>17g of fat</b>. 1 person were impressed by this recipe. From preparation to the plate, this recipe takes roughly <b>1 hour and 35 minutes</b>. This recipe from Food and Wine requires sesame seeds, lime juice, olive oil, and salmon fillets. Taking all factors into account, this recipe <b>earns a spoonacular score of 92%</b>, which is great. If you like this recipe, take a look at these similar recipes: Soy-and-Ginger-Glazed Salmon with Udon Noodles, Soy-and-Ginger-Glazed Salmon with Udon Noodles, and Soy-and-Ginger-Glazed Salmon with Udon Noodles."
}
The summary is a HTML formatted text and im using HTML.fromHtml() to display the displayable styled text. My question is, is it possible to change the properties inside the HTML such as text decoration of a link to another color?
RecipeDetails.java
private final RecipeDetailsListeners recipeDetailsListeners = new RecipeDetailsListeners() {
#SuppressLint("SetTextI18n")
#Override
public void didFetch(RecipeDetailsResponse response, String message) {
Picasso.get().load(response.image).into(recipeImage);
RecipeName.setText(response.title);
recipeSource.setText(response.sourceName);
if (response.sourceName == null){
recipeSource.setText("Unknown");
}
recipeServing.setText(response.servings + " Serving");
recipeTimePrepared.setText(response.readyInMinutes + " Min");
recipeScore.setText(String.valueOf(response.aggregateLikes) + " Likes");
recipeSummary.setText(Html.fromHtml(response.summary, Html.FROM_HTML_MODE_COMPACT));
IngredientListRecipeDetails.setHasFixedSize(true);
IngredientListRecipeDetails.setLayoutManager(new LinearLayoutManager(getBaseContext(), LinearLayoutManager.VERTICAL, false ));
adapter = new recipeDetailsIngredientAdapter(getBaseContext(), response.extendedIngredients);
IngredientListRecipeDetails.setAdapter(adapter);
}
#Override
public void didError(String message) {
customToast("Unsuccessful", message, "Failure");
}
};
I am making an app that displays daily stock data to the user. The problem is, I am using retrofit to make the API calls. With this type of JSON, I would need a POJO with every stock symbol. This obviously isn't possible.
I want to be able to do something along the lines of - listOfStocks[1].getQuote().getLatestPrice();
This is the kind of JSON data that I am receiving.
{
"AAPL":{
"quote":{
"symbol":"AAPL",
"companyName":"Apple Inc.",
"primaryExchange":"Nasdaq Global Select",
"sector":"Technology",
"calculationPrice":"close",
"open":156.21,
"openTime":1548772200635,
"close":154.68,
"closeTime":1548795600703,
"high":158.13,
"low":154.11,
"latestPrice":154.68,
"latestSource":"Close",
"latestTime":"January 29, 2019",
"latestUpdate":1548795600703,
"latestVolume":39402866,
"iexRealtimePrice":161.04,
"iexRealtimeSize":90,
"iexLastUpdated":1548799005088,
"delayedPrice":154.68,
"delayedPriceTime":1548795600703,
"extendedPrice":160.6,
"extendedChange":5.92,
"extendedChangePercent":0.03827,
"extendedPriceTime":1548799199389,
"previousClose":156.3,
"change":-1.62,
"changePercent":-0.01036,
"iexMarketPercent":0.02338,
"iexVolume":921239,
"avgTotalVolume":42308638,
"iexBidPrice":0,
"iexBidSize":0,
"iexAskPrice":0,
"iexAskSize":0,
"marketCap":731605928040,
"peRatio":13.03,
"week52High":233.47,
"week52Low":142,
"ytdChange":-0.030876717325227843
}
},
"FB":{
"quote":{
"symbol":"FB",
"companyName":"Facebook Inc.",
"primaryExchange":"Nasdaq Global Select",
"sector":"Technology",
"calculationPrice":"close",
"open":148,
"openTime":1548772200837,
"close":144.19,
"closeTime":1548795600692,
"high":148.1,
"low":143.43,
"latestPrice":144.19,
"latestSource":"Close",
"latestTime":"January 29, 2019",
"latestUpdate":1548795600692,
"latestVolume":17353892,
"iexRealtimePrice":144.25,
"iexRealtimeSize":100,
"iexLastUpdated":1548796485236,
"delayedPrice":144.19,
"delayedPriceTime":1548795600692,
"extendedPrice":145.05,
"extendedChange":0.86,
"extendedChangePercent":0.00596,
"extendedPriceTime":1548799197301,
"previousClose":147.47,
"change":-3.28,
"changePercent":-0.02224,
"iexMarketPercent":0.02505,
"iexVolume":434715,
"avgTotalVolume":25773532,
"iexBidPrice":0,
"iexBidSize":0,
"iexAskPrice":0,
"iexAskSize":0,
"marketCap":414371435774,
"peRatio":19.51,
"week52High":218.62,
"week52Low":123.02,
"ytdChange":0.04048110849056598
}
},
"MSFT":{
"quote":{
"symbol":"MSFT",
"companyName":"Microsoft Corporation",
"primaryExchange":"Nasdaq Global Select",
"sector":"Technology",
"calculationPrice":"close",
"open":104.88,
"openTime":1548772200862,
"close":102.94,
"closeTime":1548795600515,
"high":104.97,
"low":102.17,
"latestPrice":102.94,
"latestSource":"Close",
"latestTime":"January 29, 2019",
"latestUpdate":1548795600515,
"latestVolume":31105047,
"iexRealtimePrice":102.895,
"iexRealtimeSize":100,
"iexLastUpdated":1548795598662,
"delayedPrice":102.94,
"delayedPriceTime":1548795600515,
"extendedPrice":103.67,
"extendedChange":0.73,
"extendedChangePercent":0.00709,
"extendedPriceTime":1548799195514,
"previousClose":105.08,
"change":-2.14,
"changePercent":-0.02037,
"iexMarketPercent":0.03309,
"iexVolume":1029266,
"avgTotalVolume":40670438,
"iexBidPrice":0,
"iexBidSize":0,
"iexAskPrice":0,
"iexAskSize":0,
"marketCap":790189956684,
"peRatio":26.53,
"week52High":116.18,
"week52Low":83.83,
"ytdChange":-0.002371582278481079
}
}
}
Personally I'd make a model for quote and symbol then use a Map<String, Symbol>.
Here's an example where the model only has the latestPrice property to shorten the example:
class Quote {
#SerializedName("latestPrice")
private double latestPrice;
// ...
}
class Symbol {
#SerializedName("quote")
private Quote quote;
//...
}
Now making the Retrofit call use Map<String, Symbol> should give you a map where you can iterate over the entries and get the latest price of each of them.
I'm trying to parse in android studio a JSON, that containts this :
"stops":
[
{
"num": 1,
"time": "2016-04-27T06:15:00.000Z",
"title":"Flight to London",
"desc":"Barcelona BCN-London-Gatwick LGW",
"type":"0",
"subtype":0
},
{
"num": 2,
"time": "2016-04-27T10:35:00.000Z",
"title":"Gatwick express",
"desc":"From Airport to London",
"type":"0",
"subtype":1
},
{
"num": 3,
"time": "2016-04-27T12:15:00.000Z",
"title":"Pub the black horse",
"desc":"From Airport to London",
"type":1,
"subtype":1,
"location": "51.476334, -0.062700",
"images": [ "https://fitzrovianews.files.wordpress.com/2011/01/black_horse_rathbone_pl.jpg"
]
},
{
"num": 4,
"time": "2016-04-27T12:16:47.000Z",
"title":"The Tower Bridge",
"desc":"# The Tower Bridge Facts\n## Architecture\n**Tower Bridge** is a combined bascule and suspension bridge in London built in _1886–1894_. The bridge crosses the River Thames close to the Tower of London and has become an iconic symbol of London. Tower Bridge is one of five London bridges now owned and maintained by the Bridge House Estates, a charitable trust overseen by the City of London Corporation. \n>It is the only one of the Trust's bridges not to connect the City of London directly to the Southwark bank, as its northern landfall is in Tower Hamlets.\n## The bridge Exhibition\nThis must-see London attraction invites you to step inside the most famous bridge in the world to explore its iconic structure, spectacular views and glass floor, modern exhibitions and magnificent Victorian Engine Rooms! ",
"type":1,
"subtype":6,
"location": "51.507792, -0.087786",
"images": [
"https://i.ytimg.com/vi/nby0Mr2LfBQ/hqdefault.jpg",
"http://raindropsofsapphire.com/wp-content/uploads/2011/10/london-bridge.jpg",
"http://www.londonforfree.net/gizmo/wp-content/uploads/2015/02/southwark-bridge.jpg"
]
},
{
"num": 5,
"time": "2016-04-27T12:18:10.000Z",
"title":"St. Paul Cathedral",
"desc":"# HISTORY \nSt **Paul's Cathedral**, London, is an _Anglican cathedral_, the seat of the _Bishop of London_ and the mother church of the Diocese of London. \n * It sits on Ludgate Hill at the highest point of the City of London and is a Grade 1 listed building. \n * Its dedication to Paul the Apostle dates back to the original church on this site, founded in AD 604.",
"type":1,
"subtype":6,
"location": "51.513825, -0.098351",
"images": [
"https://d1wgio6yfhqlw1.cloudfront.net/sysimages/product/resized6/Interior_St_Pauls_Cathedral_132_12992.jpg",
"https://d1kioxk2jrdjp.cloudfront.net/resized/486x324/48-st_pauls_ctahedral_millenirm_bridge.jpg",
"http://i4.mirror.co.uk/incoming/article8299330.ece/ALTERNATES/s615b/LOND-2016-052-HMQ-St-Pauls-Thanks-Giving-704JPG.jpg"
]
}
]
The problem is, i don't know how to deal with the field "location" or "images" which are optional. I know how to deal with the first "stop", i'm doing this :
JSONArray stops = jsonObj.getJSONArray("stops");
for (int i = 0; i < stops.length(); i++) {
JSONObject c = stops.getJSONObject(i);
String num = c.getString("num");
String time = c.getString("time");
String title = c.getString("title");
String descripcion = c.getString("desc");
String type = c.getString("type");
String subtype = c.getString("subtype");
......
}
But i don't know how to check it here is a elment location or a jsonArray "images"...
best way to handle optional fields in JSON is to use opt instead of get
opt provides the parsed value if exist or default value for that datatype if requested key does not exist.
best thing is, it don't even need a try catch block since it always returns a value and in case of any error from server, it will not let your app crash or prevent other values from being parsed.
String location = response.optString("location");
if location exist in response, then it will initialize with the value or it will leave the string null. in case of int or long default is 0, in case of boolean default is false. read about opt for more details.
Use has() method
JSONArray stops = jsonObj.getJSONArray("stops");
for (int i = 0; i < stops.length(); i++) {
JSONObject c = stops.getJSONObject(i);
String num = c.getString("num");
String time = c.getString("time");
String title = c.getString("title");
String descripcion = c.getString("desc");
String type = c.getString("type");
String subtype = c.getString("subtype");
if(c.has("location") && !c.isNull("location")){
// parse location
}
if(c.has("images") && !c.isNull("images")){
// parse images
}
.....
}
you should use Retrofit and Google Gson, where you do not have to do lot of work,
they will do the job for you.
look at these
https://guides.codepath.com/android/Consuming-APIs-with-Retrofit
http://www.vogella.com/tutorials/Retrofit/article.html
Is it possible to get higher quality images from discogs api? Right now I can only get a thumbnail image of size 150x150. Is there any way for developers to get a better quality image? The JSON only has link for thumnail images.
So if this is my request
https://api.discogs.com/database/search?page=1&per_page=1&key=my_key&secret=my_secret&q=Waka%20Waka
This is the response that I get
{
"pagination":{
"per_page":1,
"items":1731,
"page":1,
"urls":{
"last":"https://api.discogs.com/database/search?q=Waka+Waka&per_page=1&secret=my_secret&page=1000&key=my_key",
"next":"https://api.discogs.com/database/search?q=Waka+Waka&per_page=1&secret=my_secret&page=2&key=my_key"
},
"pages":1000
},
"results":[
{
"style":[
"Alternative Rock",
"Jazz-Rock",
"Fusion"
],
"thumb":"https://api-img.discogs.com/YokvweulGO7tRuNoSv3b1F4UntQ=/fit-in/150x150/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/R-1378139-1466530254-6813.jpeg.jpg",
"format":[
"Vinyl",
"LP",
"Album"
],
"country":"US",
"barcode":[
"(31,456) RE I",
"(31,457) RE I",
"MS-2094 31456-1",
"MS-2094 31457-1",
"S1\u00b7MS 2094A 31456-Re1-T2 ",
"25 MS 20941 31457 Re1\u00b7T2",
"ASCAP"
],
"uri":"/Frank-Zappa-Waka-Jawaka-Hot-Rats/master/35744",
"community":{
"have":3089,
"want":1992
},
"label":[
"Bizarre Records",
"Reprise Records",
"Warner Bros. Records Inc.",
"Warner Bros. - Seven Arts Records, Inc.",
"Warner Bros. Records Inc.",
"Warner Bros. Records Inc.",
"Munchkin Music",
"Paramount Studios"
],
"catno":"MS 2094",
"year":"1972",
"genre":[
"Rock"
],
"title":"Frank Zappa - Waka / Jawaka \u2022 Hot Rats",
"resource_url":"https://api.discogs.com/masters/35744",
"type":"master",
"id":35744
}
]
}
I want to have larger images, how can I do that? Is that even possible? Thanks !!
When you search for releases you get an id that you can use to get more release info.
Make call to https://api.discogs.com/releases/{id}?key={your_key}&secret={your_secret}
Response usually has some image urls that you can download.
i have worked with json name value pair before works fine and great but below is my json object in which there is no name and only value. Can anyone tell is it possible to use this type of json in android if yes then how to extract values from this json object. or any related link will be great.
JSON OBJECT
{
"log_list": [
[
"21",
"doctorFisher",
"Pharmacy 1",
"patientfish",
"test",
"2014-08-25 05:58:18",
"record UCI tech FDNY icky shut rack soon sun TDK tell. ox it'll ohm URL did GCVO dash ugly dog did flood idiot fluff if if rid t-shirts didn't TechTV only chef doc scotch Rebekah an if lb tax scotch am ICN JCB JCB HGV JCB HGV in on pm tax UDC OK red uh HK ohm",
"<img id=\"prescription_image\" onClick=showPrescriptionDetails(\"3c59dc048e8850243be8079a5c74d079\") onmouseover=\"this.style.cursor='pointer'\" src=\"\" width=\"20\" height=\"20\">"
],
[
"20",
"doctortest12345",
"Pharmacy 1",
"testpatient12345",
"test",
"2014-08-25 03:57:32",
"she'd Urdu text scuff uno dad uno each ink why tough days ICO saved USCIS David rig though end FDIC UCI's for USCIS tend did for dog such vidi fly floor exited did DND hand bid GMD.",
"<img id=\"prescription_image\" onClick=showPrescriptionDetails(\"98f13708210194c475687be6106a3b84\") onmouseover=\"this.style.cursor='pointer'\" width=\"20\" height=\"20\">"
]
}
thanks in advance
Yes, that format would work and you can pretty easily use it.
Assuming you have a variable String json which contains that data:
JSONObject jsonObj = new JSONObject(json);
Now log_list would be parsed as a JSONArray like so:
JSONArray logJsonArray = jsonObj.getJSONArray("log_list");
Then you can iterate through that outer array very similar to how you would a normal array or ArrayList:
for(int outerArrayPos = 0; outerArrayPos < logJsonArray.length(); outerArrayPos++){
JSONArray innerArray = logJsonArray.getJSONArray(outerArrayPos);
for(int innerArrayPos = 0; innerArrayPos < innerArray.length(); innerArrayPos++){
String valueAtIndex = innerArray.getString(innerArrayPos); //innerArray
// Do whatever you want with the values here
}
}
That will parse your String, but figuring out how to actually use that data is going to be most of your struggle, so I hope the order in which the information is returned will always be the same for your sake.
If you can, I would definitely recommend using a JSONObject for the data that is currently contained in your inner array. e.g.
{
"log_list": [
{
"id": "21",
"name": "doctorFisher",
.....
}
]
}