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.
Related
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
I have a json array data file (pre-prepared) each item in the json array contain data which I want to be able to use in order to send a request to a server using the json array data file , i know how to be able to send one request after extracting data from the file but I am struggling of getting all the data to send one after another.
I want to imitate the following behavior which I wrote already in java:
List<Integer> mtl = Arrays.asList(new Integer[]{1, 9, 257, 265});
for(int i=0;i<jsonArray.size();i++){
JSONObject item = (JSONObject)jsonArray.get(i);
int dataFlagType = Integer.parseInt(item.get("DataFlagType").toString());
if(!(mtl.contains(dataFlagType))){
sendPushStream(Long.parseLong(m_ap.sid),pid,subsId,item,domain,dnsName,dataFlagType);
}else{
lastMessage = (JSONObject)jsonArray.get(i);
}
Thread.sleep(100);
}
Thread.sleep(100);
sendPushStream(Long.parseLong(m_ap.sid),pid,subsId,lastMessage,domain,dnsName,Integer.parseInt(lastMessage.get("DataFlagType").toString()));
where sendPushStream execute the post request itself.
I would suggest organizing your Test Plan as follows:
HTTP Request (Protocol: file, Path: /path/to/your/file.json
JSON Extractor (relevant JSONPath query to extract values and store them into JMeter Variables)
ForEach Controller to iterate the Variables coming from the JSON Extractor
HTTP Request - to mimic sendPushStream
Another option is using your Java code from JSR223 Sampler or JUnit Request sampler, just make sure you package your helper code as .jar and add it to JMeter Classpath
I am trying to add the source URL on an existing resource but I can't get it to work.
This is what I have:
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("id", new StringBody("resource id",ContentType.TEXT_PLAIN))
.addPart("Source", new StringBody("www.google.com",ContentType.TEXT_PLAIN))
.build();
postRequest = new HttpPost(host+"/api/3/action/resource_update");
postRequest.setEntity(reqEntity);
postRequest.setHeader("X-CKAN-API-Key", "myApi");
Which gives me the following error:
"success": false, "error": {"__type": "Validation Error", "url": ["Missing value"]}
I can add a Source field when I upload a file, but it doesn't create a link for it. Maybe because I use ContentType.TEXT_PLAIN instead of something that will tell CKAN that this is a link?
Edit:
By source I mean the link on the resource information where it points to the webpage of the original data. When manually uploading a file you have that option, I just cant get it to work with the API.
The error is telling you that you've not specified the url value for the resource, which is necessary.
The content that you POST needs to be in JSON format, whereas you appear to be sending it as multipart form data.
The JSON keys you need to send for a resource are id, url, name, format etc. i.e. you need to set url rather than Source. You can see the existing keys by looking at /api/action/resource_show?id=xyz.
The normal way to use resource_update is to do resource_show first, edit the JSON with the new values, and then resource_update. That way you don't lose any other properties of the resource, such as format.
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.
I have to do a HTTP post in java every second after building a json object.
The json object is built from reading a CSV file which is huge (200Mbs+), so my problem is
how do I read x number of lines build x objects and post that every second(as it is not possible to parse the whole 200mb file in less than a second) and continue reading the next x lines.
Please let me know your thoughts..
Can I use Java timer class, and keep reading the CSV file and at the same time post the json object to the server every second with the formed json?
It is hardly possible to read, parse, convert and send a 200 MB file once per second.
So you need to change your design:
My suggestion would be to only send changed lines, something like this:
{
"1" : {"field1":"value1","field2":"value2"},
"17" : {"field1":"value1","field2":"value2"}
}
Which of course gives you new problems:
The client needs to figure out which lines have changed, and the server needs to integrate the changed lines with the existing data.
I would make it depending on the file size and not depending on time.
BufferedReader fin = null; //create it
Gson gson=new Gson(); //Google code open source library for JSON in Java
ArrayList<JSONObject> jsonList=new ArrayList<JSONObject>();
while (((line = fin.readLine()) != null)) {
if ( line.length()==0 ){
//"Blank line;
}else{
currJSON=loadJSON(line);//You have to load it in a Java Object
if ( jsonList.size()<MAX_JSON){
jsonList.add(currJSON);
}
if (JsonList.size()==MAX_JSON){ //Define the maximum size of the list you want to post
gson.toJson(jsonList); //Convert to JSON
//You should post your Json with some Http Connection to your server
jsonList.clear();