I would like to connect to a Api url, retrieve the json and store everything in a object list. Here is an example of what the url can return as Json. Notice at the beginning there is a count, page and last_page. These are how many items there are on the page, what page you are on and how many pages there are in total (max 50 counts on 1 page). A unspecific search could easily return up to 1000 pages of results
I have this code in c# which works flawlessly but after searching and trying for a long time i have no idea how to recreate this in Android java.
This is api_handler.cs
public class api_Handler
{
public static RootObject objFromApi_idToName(string spidyApiUrl, int page){
RootObject rootObject = null;
RootObject tempRootObject = null;
do{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(spidyApiUrl + "/" + page);
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream()){
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
var jsonReader = new JsonTextReader(reader);
var serializer = new JsonSerializer();
tempRootObject = serializer.Deserialize<RootObject>(jsonReader);
if (rootObject == null){
rootObject = tempRootObject;
}
else{
rootObject.results.AddRange(tempRootObject.results);
rootObject.count += tempRootObject.count;
}
}
page++;
}
while (tempRootObject != null && tempRootObject.last_page != tempRootObject.page);
return rootObject;
}
}
Which i called in the main_form.cs like this
// url will become = http://www.gw2spidy.com/api/v0.9/json/item-search/ + textbox.text
// full example = http://www.gw2spidy.com/api/v0.9/json/item-search/Sunrise
string spidyApiUrl = String.Format("{0}{1}/api/{2}/{3}/{4}/{5}", Http, spidyHost, spidyApiVersion, format, spidyApiType, dataId);
var spidyApi_idByName = api_Handler.objFromApi_idToName(spidyApiUrl, startPage);
And ofcourse the constructors though I would assume these arent really important to include in the question.
public class Result
{
public int data_id { get; set; }
public string name { get; set; }
public int rarity { get; set; }
public int restriction_level { get; set; }
public string img { get; set; }
public int type_id { get; set; }
public int sub_type_id { get; set; }
public string price_last_changed { get; set; }
public int max_offer_unit_price { get; set; }
public int min_sale_unit_price { get; set; }
public int offer_availability { get; set; }
public int sale_availability { get; set; }
public int sale_price_change_last_hour { get; set; }
public int offer_price_change_last_hour { get; set; }
}
public class RootObject
{
public int count { get; set; }
public int page { get; set; }
public int last_page { get; set; }
public int total { get; set; }
public List<Result> results { get; set; }
}
How can i turn this working code in c# to android java? Or is it a better idea to start all over from scratch (i would still require some guidance as i have tried quite few times without success)
Also:
in C# on a pc this would run in a perfect acceptable time maybe load 1-2 sec for a 1000x50 objects which is a very unspecific search. These unspecific searches can ofcourse happen since its determined from user input but it wont happen too often, a normal search could be from 1 page to 50. is a phonecapable of doing this in a acceptable time?
TL;DR Connect to api > retrieve all json values > store all in object list > send back to activity.java