I was wondering, I"m looking to add update button in my fragment which when user click will check if updates available or not .
I"m looking for solution on How to acess json file from url and check if json value contains new value or not . if key matches to the already specified value then display message you data has been updated and if not then display No updates availaible.
For Example
if below is the json file
[
{
"updateKey": "1"
}
]
and if it contains other then 1 then show toast message with updates availaible.
To access data from a url you can use retrofit https://square.github.io/retrofit/
when you get e JSON response you can verify if exist key using JSONObject
json = new JSONObject(response);
if (json.has(<key>)) { }
Something similar to this one https://stackoverflow.com/a/43341909/10931055
Related
In the app, I'm currently making, I need to read a CSV file from a downloadable link and show its data.
For example, consider this link: https://api.covid19india.org/csv/latest/case_time_series.csv
If you click on the link, it'll download a CSV file.
So, what I want to do is, when the user opens the app, I want to access the data in this CSV link, parse it and show the data on the screen in a recycler view.
How to do that?
I found this Kotlin code as one of the way to get the data in CSV by using Volley's stringRequest. In this case we get all the data as a string with rows being separated by \n and data in a row being separated by commas(,)
For this sample code, I'm accessing date from this URL: https://sample-videos.com/csv/Sample-Spreadsheet-10-rows.csv
val queue = Volley.newRequestQueue(this)
val url = "https://sample-videos.com/csv/Sample-Spreadsheet-10-rows.csv"
val stringRequest = StringRequest(
Request.Method.GET, url,
{ response ->
// Display the first 500 characters of the response string.
binding.csvDataTextView.text = "Response is: ${response.substring(0, 500)}"
val allLinesInResponse = response.substring(0)
var rowsData: List<String> = allLinesInResponse.split("\n")
Log.d("abc", "The Volley request worked")
},
{
Log.d("abc", "The Volley request didn't work!")
})
queue.add(stringRequest)
queue.start()
There may be other better ways, but this is one of those which work.
I'm using Jackson 2.6.5.
I'm trying to read the following JSON:
{
"metadata1":"value",
"metadata2":"value"
}
{
"field1":"value",
"field2":"value",
....
}
With the following code:
JSONObject jsonObj = new JSONObject(jsonString);
But jsonObj contains only the "first" part of my JSON (the metadata), How can I read the "second" part of my JSON? (the part with the fields)?
EDIT
I know that my JSON doesn't contain "," so how can I parse it without "," between the jsons?
Seems like the JSON you added is missing a , sign between the two objects.
If you get such an non-json as input string, you might want to consider to:
First manipulate the input string by adding it the missing , . E.g. find the location of }{ and replace it with },{
And only then to insert it to the jackson
Your json is invalid, so you can't parse invalid json scheme by json parsing tools out of the box,
Instead you could read file by yourself into 2 strings - one valid json per string and then parse it with any json parser.
I have a JSON data in the properties file and trying to retrieve it in java. When I am trying to retrieve the JSON data with the property name it's giving only first string/word from the JSON.
Inside the property file, I have the below content.
profile: {"fname": "ABC","lname": "XYZ","meetings":{"morning":10,"evening":60}}
I am trying to read the content using property name 'profile' as a string and I am getting below error message.
Expected ',' instead of ''
can someone help me with the issue, I tried to escape and unescape but still have the same issue
It may depend on what you are using to deserialize the JSON, but well formed JSON is a single element, so what you have needs to be inside of a container. That is, your file content should be:
{ profile: {"fname": "ABC","lname": "XYZ","meetings":{"morning":10,"evening":60}}}
You can do it like this:
profile={"fname": "ABC","lname": "XYZ","meetings":{"morning":10,"evening":60}}
Or if you want to do it in multiple lines
profile={\
"fname": "ABC",\
"lname": "XYZ",\
"meetings":{\
"morning":10,\
"evening":60\
}\
}
I am getting some JSON values from the server but I don't know if there will be a particular field or not.I need to validate based on the key .
One Type Of Response
Another Type Of Response
In AysncTask we can use "has" function but in Retrofit i am unable to find a solution .
Provide me a solution
Retrofit will parse all attributes that you specified in your model. If some attribute in your JSON does not exist Retrofit will set NULL as the value of that attribute.
Knowing this feature, the only thing that you need to do is something like:
if(myObject.getReviewerDetails() == null)
// do something
Happy code!
You can check key exist or not using jsonObject.has like following way,
JSONObject jsonObject=new JSONObject();
if(jsonObject.has("reviewer_details")){
//do process with data
}
I am trying to read RSS feeds from CNN in an Android project. So far everything is going right. I successfully made the connection and retrieved the whole XML file as one string. Then I tried to create a JSON Object and parse it. However some part of it couldn't be read. The XML I tried to read is this
view-source:http://rss.cnn.com/rss/edition_world.rss
In order to simplify and make everything more clear, I attached a picture of the RSS feed viewed in a JSON Object Editor:
http://tinypic.com/view.php?pic=2iiaezb&s=8#.U7ax0vmGFmw
So the code is like this,
//It successfully converts the text to JSON
JSONObject jObj = new JSONObject(responseText);
String respTime = jObj.getString("responseTime");
//It successfully prints the responseTime
System.out.println("Response time is: " + respTime);
JSONObject respHeader = jObj.getJSONObject("responseHeaders");
String date = respHeader.getString("date");
//It successfully prints the date as well
System.out.println("Date is: "+ date);
//However it says no value for rss found
JSONObject rssObj = jObj.getJSONObject("rss");
JSONObject channelObj = rssObj.getJSONObject("channel");
JSONArray itemArr = channelObj.getJSONArray("item");
"responseTime", "responseHeaders" and "rss" are all equivalent in terms of hierarchical structure of the XML file, as you can see from the image I referenced. So while I am able to read "responseTime" and "responseHeaders", why does it say that no value found for "rss", and therefore I am unable to reach any of it's sub-items ?
I found out the answer. The actual JSON object string, namely the responseText, is not identical to what appears in the XML file. It is something different and it took time for me to realize it. I couldn't copy it from the Eclipse's logcat since it has a limited buffer, so I had to study it by writing to a text file. Then I could parse it correctly.