java - Loop AsyncTask class and fetch Json and store as object in list -


i want connect api returns me json values depending on enter request. json structure same values these properties differ. these json values constructed object list , there user further in application.

with have working code connects , reads json values httpentity , stores these in new jsonobject. object send function object gets stripped , constructed object list later use in application.

this code working. except api link shows 50 results, depending on entered get request. when there more 50 results new page created can accessed number @ end of url. these pages exist (dont have user search request) have added object list aswell.

question: how loop through extended asynctask class new url fetch json data from?

i again note code works single page can not figure out how loop through existing pages , add these same object list.

we start in activity.java

string usersearchrequest = search_activity_data.getstring("usersearchrequest"); string url = "http://www.gw2spidy.com/api/v0.9/json/item-search/" + usersearchrequest + "/"; // example api url: //http://www.gw2spidy.com/api/v0.9/json/item-search/iron/0" //the last number page viewing, every 50 results new page created. 51 results there page 0 , 1 access.  asyncfetch asyncfetch = new asyncfetch(this); asyncfetch.setonresponse(this); asyncfetch.execute(url); 

from here url passed asyncfetch class function doinbackground params.

public class asyncfetch extends asynctask<string, void, jsonobject> {      public asyncfetch(context context) {         this.context = context;     }      private context context;     private jsonobject jsonobject;     private onresponse onresponse;      public void setonresponse (onresponse onresponse) {         this.onresponse = onresponse;     }      @override     protected jsonobject doinbackground(string... params ) { //incompatible return type         // todo auto-generated method stub          try {             httpget = new httpget(params[0]);             httpclient client = new defaulthttpclient();             httpresponse response = client.execute(get);             httpentity entity = response.getentity();             string result = entityutils.tostring(entity);             jsonobject = new jsonobject(result);          } catch (clientprotocolexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (jsonexception e) {             // todo auto-generated catch block             e.printstacktrace();         }          return jsonobject;     }      @override     protected void onpostexecute(jsonobject result) {         // todo auto-generated method stub         super.onpostexecute(result);         this.onresponse.onresponse(result);     }      public interface onresponse {         public void onresponse(jsonobject object);     }   } 

in onpostexecute in asyncfetch class object send activity.java

public void onresponse(jsonobject object) {         log.i("gw2log", object.tostring());          apirootobject resultclass = new apirootobject();          try {             resultclass.setcount(object.getint("count"));             resultclass.setpage(object.getint("page"));             resultclass.setlast_page(object.getint("last_page"));             resultclass.settotal(object.getint("total"));             jsonarray list = new jsonarray(object.getstring("results"));              (int = 0; < resultclass.getcount(); i++) {                 jsonobject resultsobject = list.getjsonobject(i);                 apiresults temp = new apiresults();                 temp.setdata_id(resultsobject                         .getint("data_id"));                 temp.setname(resultsobject                         .getstring("name"));                 temp.setrarity(resultsobject                         .getint("rarity"));                 temp.setrestriction_level(resultsobject                         .getint("restriction_level"));                 temp.setimg(resultsobject                         .getstring("img"));                 temp.settype_id(resultsobject                         .getint("type_id"));                 temp.setsub_type_id(resultsobject                         .getint("sub_type_id"));                 temp.setprice_last_changed(resultsobject                         .getstring("price_last_changed"));                 temp.setmax_offer_unit_price(resultsobject                         .getint("max_offer_unit_price"));                 temp.setmin_sale_unit_price(resultsobject                         .getint("min_sale_unit_price"));                 temp.setoffer_availability(resultsobject                         .getint("offer_availability"));                 temp.setsale_availability(resultsobject                         .getint("sale_availability"));                 temp.setsale_price_change_last_hour(resultsobject                         .getint("sale_price_change_last_hour"));                 temp.setoffer_price_change_last_hour(resultsobject                         .getint("offer_price_change_last_hour"));                 resultclass.addobject(temp);             }          } catch (jsonexception e) {             // todo auto-generated catch block             e.printstacktrace();         }          for(int = 0; < resultclass.count; i++) {             log.i("gw2log", resultclass.getobject(i).name);          }      } 

from here can access resultclass list , loop through objects , display properties , values.

how can make work multiple pages exact same json structure?

edit: have code works in c#. it's exact same project of mine in android java. goal same can't working

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;         } 

well, don't think that's idea you'r trying do, because can have outofmemoryerror if amount of server data big.

otherwise, suggestion load data:

1) make 2 class variable (in activity), first number of pages , second counter:

private static final int pages_number = 38; // think last page  38 in api private int mpagescounter = 0; 

2) add jsonobject class variable (in activity) save pages:

jsonobject malldata = new jsonobject(); 

3) change onpostexecute() call onresponse() if pages loaded:

    @override         protected void onpostexecute(jsonobject result) {             super.onpostexecute(result);              malldata.put("page"+mpagescounter, result);// save current page "pagex" key             mpagescounter++;              if(mpagescounter <= pages_number){// create task load next page                  asyncfetch asyncfetch = new asyncfetch(youractivity.this);                  asyncfetch.setonresponse(youractivity.this);                  asyncfetch.execute(url+mpagescounter);             } else {// pages loaded -> call onresponse() method                   this.onresponse.onresponse(malldata);             }         } 

4) change onresponse() method handle new json format.

to loop pages can that:

    iterator = object.keys();     while (it.hasnext()) {         string key = (string)it.next();         try {             jsonobject jsonpage = object.get(key);// here page             // rest of stuff page         } catch (jsonexception e) {             e.printstacktrace();         }     } 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -