How to add the data from an api without Pojo - android

I am getting data from an api which cannot be converted into pojo so am not able to get the data in a normal manner
Data that i am getting
{"TABLE_DATA":"
{\"data\":[
[\"Tiger Nixon\",\"System
Architect\",\"Edinburgh\",\"5421\",\"2011/04/25\",\"$320,800\"],
[\"Garrett
Winters\",\"Accountant\",\"Tokyo\",\"8422\",\"2011/07/25\",\"$170,750\"],
[\"Ashton Cox\",\"Junior Technical
Author\",\"SanFrancisco\",\"1562\",\"2009/01/12\",\"$86,000\"],
[\"Cedric Kelly\",\"Senior Javascript
Developer\",\"Edinburgh\",\"6224\",\"2012/03/29\",\"$433,060\"],
[\"Airi
Satou\",\"Accountant\",\"Tokyo\",\"5407\",\"2008/11/28\",\"$162,700\"],
[\"Brielle Williamson\",\"Integration Specialist\",\"New
York\",\"4804\",\"2012/12/02\",\"$372,000\"],
[\"Herrod Chandler\",\"Sales Assistant\",\"San
Francisco\",\"9608\",\"2012/08/06\",\"$137,500\"],
[\"Rhona Davidson\",\"Integration
Specialist\",\"Tokyo\",\"6200\",\"2010/10/14\",\"$327,900\"],
[\"Colleen Hurst\",\"Javascript Developer\",\"San
Francisco\",\"2360\",\"2009/09/15\",\"$205,500\"],
[\"Sonya Frost\",\"Software
Engineer\",\"Edinburgh\",\"1667\",\"2008/12/13\",\"$103,600\"],
[\"Jena Gaines\",\"Office
Manager\",\"London\",\"3814\",\"2008/12/19\",\"$90,560\"]`
there is no pojo available This is my first time working in an API any guide will be helpful. I am receiving the following data using retofit and rxjava2.
Data format is post method .

This is a jquery format. You may try to use this https://javacodegeeks.com/2013/02/jquery-datatables-and-java-integration.html
Look also this topic stackoverflow.com/questions/48942253/how-to-implement-jquery-datatable-in-android-any-library
The service you get data from may have an opportunity to set data format, you must specify it in your GET query, look in the API documentation.

That looks a lot like a .csv file. CSV files were the old format of Comma Separated Values, built from Tabular data.
Each row is one object and each column is its attribute/field. You can read about CSV files here.
Approach 1
Filter out using string manipulation the extra symbols of slashes and quotes. Delimit at the square braces (more info here) and then reading each delimited comma separated value text by any CSV reading library. Here's a tutorial using Apache Commons CSV library to read CSV files in Java.
Approach 2
Instead of using any library, if your response set is small and you don't need as much parsing optimization, write your object class's constructor to take in each row, filter out the useless symbols (using string manipulation) and initialize your object's fields.
All steps involved:
Clean response and remove unnecessary symbols using string manipulation.
Build a POJO class to keep one row of the dataset. Have its constructor to take in the first row of your dataset to initialize its attributes.
Build a list format or ArrayList format of your previous class with additional methods to sort, search or call by indices as required.
Build a constructor for this list object class to read in your cleaned string response and iterate over it to build Employee objects and adding them to your list.

Related

Hello, I am receiving a json string, I need to separate data by class properties

I am receiving a json string, I need to separate data by class properties. The problem is that I can only know part of the key. For example, the key is 12345#666.777, I only know #666.777. Is it possible to somehow use regular expressions or look for values by part of the key?
I would iterate over all the keys in the JSON object as shown here: https://discuss.kotlinlang.org/t/iterating-over-json-properties/1940/2
and check for every key if it contains the partial key I have as a substring using this standard Kotlin method:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/contains.html
You also need to treat cases where the same substring is present in 2 different keys (if you think that is possible).

Storing a data - Benefit and cons of each storage types

I have this kind of data. This can be or don't be an array. Just for easy reference.
ArrayName = Array1, Array2, Array3
Array1 = abc, cde, fgh
Array2 = abc, cde
Array3 = abc, cde, fgh, ijk, lmn
So, what are the best method to store this kind of data.
If I want to
Add or delete Array1 and all things inside
Add or delete item in Array2(eg. adding fgh or remove cde)
Methods I discovered:
SharedPreference Android Shared preferences example
Arrays
SQLite Android SQLite Example
Text file
Please share the pro and cons of why you choose the method.
Please also share if there are better ways to store this kind of data.
Kindly edit this if you found a better link or sample for other to reference.
Here are pros and cons for each solutions:
1) SharedPreference
You save simple key-value pairs here. So it is very hard to save array and complex structures in SharedPreference. So the solution will not work with arrays and arrays of arrays. It will be extremely(but not impossible) difficult to achieve what you want.
2) Arrays
Absolutely not! It is memory storage, so when you close app, or on process death, you will lose all data
3) SqlLite
I would add to this other Databases for android, like Realm.
Good solution. It is structured storage for collection of data. It will be very easy for storing/retrieving data when it is structured as rows. Furthermore you can delete rows easily. You don't have to read whole structure (other arrays) when you need particular row, or particular array (table in this case)
4) TextFile
I don't recommend to store in a text file, but it is possible to do so, you can serialize those arrays to text file, and deserialize. But every time you have to do this, and to read whole structure and parse it even if you want only e.g. Array2. It can be slow when your data becomes bigger.
It's incredibly hard to give advice with such vague requirements, you apparently have data structured as an array of arrays of strings, and you want to store it persistently on Android - and that's basically all we know.
In addition to the solutions mentioned, I would consider using GSON to store this as JSON to disk. While read/write may not have optimal performance, it's very easy to model documents with things like arrays of arrays, and we have no way of knowing your performance requirements vs ease of use.
class MyData {
public List<List<String>> data;
}
If you then have a MyData object, you could simply serialize it to a string, which could be written to a file on disk:
String json = new Gson().toJson(myData);
This would produce something like
{
"data": [
["abc", "cde", "fgh"],
["abc", "cde"],
["abc", "cde", "fgh", "ijk", "lmn"]
]
}
which could easily be written to disk using e.g. standard File and BufferedWriter. You can then read it back and deserialize using:
MyData myData = new Gson().fromJson(json, MyData.class);

JSON array order

I am using PHP as a middleman to access a MySql database and it returns the result of the query as a json string using json_encode, then display it within the TableLayout of the app, this is why order is important so I can line up the data and the headers.
After some research I found out that json does not enforce order so any time I call new JSONArray(result) it scrambles the json returned by PHP. Is there any way to preserve the order of the returned string? Or maybe I'm using the incorrect data structure on either end.
Relevant PHP result:
[{"FIELD1":"vsa","FIELD2":"dfs","FIELD3":"dsfa","FIELD4":"adsf","FIELD5":"23","ZIPCODE":"asdf","USERNAME":"asd","PASSWORD":"as","DATE1":"dsfa"}]
Relevant Android Result After JSONArray(result):
[{"ZIPCODE":"asdf","DATE1":"dsfa","FIELD3":"dsfa","FIELD2":"dfs","FIELD5":"23","FIELD4":"adsf","USERNAME":"asd","FIELD1":"vsa","PASSWORD":"as"}]
I believe the reordering inside a JSON object is due to the fact that JSON objects are key/value pairs (not an indexed array), which by default are unordered. However, the JSON array is an ordered sequence of values (JSON objects).
Don't rely on order!
Source: http://www.ietf.org/rfc/rfc4627.txt
I've never seen new JSONArray(String) change the order of anything, and I've used it a lot. However, what you have seems to be an array of length 1. Using myJsonArray.getJsonObject(0).getString("ZIPCODE") should still return the correct data, and as long as you query in the correct order (FIELD1, FIELD2, FIELD3, etc), you should be fine.

Android - Extend BasicNameValuePair

Im trying to develop an application that accesses a database through POST requests and returns values in the database.
Im curious about something tho. what if i wanted to pass a number or a list to the database, such as the list of columns to grab for each row. Is it possible to extend the BasicNameValuePair class to acomplish this, or am i missing the correct way to do this?
Extending the BasicNameValuePair is not going to help you as the parameters of a HTTP request need to be name value pairs. If you want to pass more parameters, add more name value pairs and handle them in the server.
If the number of parameters are high, it would be prudent to use HTTP POST. If the parameters are going to be complex, it would beneficial to submit the request in a more user-friendly format like JSON or XML and parse it on the server side. However, if it is just the name of the columns of the table row, you can also think of using a format like comma separated list.
http://mydomain/getdata?id=10&columns=1,2,3,4
In case of the above, you need to parse the columns from the comma separated list at the server side, whereas you are creating name-value pair for columns as follows:
BasicNameValuePair columnData = new BasicNameValuePair("columns", "1,2,3,4");

parsing html data in android

I am making an application in which i have to parse HTML data. I have got data that i want. But data is repeating five to six times. I m saving data into string, but when i am printing this string there is no repeated data. e.g data having 23 values and it is repeating five or six times. I have entered static string it is displaying fine.
Here is code:
doc = (Document) Jsoup.connect("http://altoona.craigslist.org/search/cta?query=Ford+WINDSTAR&srchType=T&minAsk=&maxAsk=").get();
System.out.println("*****DOC*****"+doc);
s1=doc.getAllElements().text().toString();
System.out.println("**************S1*************"+s1);
Please help me where m doing something wrong.
try this one http://htmlparser.sourceforge.net/
HTML Parser is a Java library used to parse HTML in either a linear or nested fashion. Primarily used for transformation or extraction, it features filters, visitors, custom tags and easy to use JavaBeans. It is a fast, robust and well tested package.

Categories

Resources