Android, Handling multiple URL for parsing -


i making simple news reader app. news need shown in 1 recyclerview, list of news. problem there multiple urls whom extract data , know how parse 1 dont know how handle more of them. here code:

public class newsactivity extends appcompatactivity {      public static final string log_tag = newsactivity.class.getsimplename();     public static final string newsurl1 = "http://tests.intellex.rs/api/v1/news/list?page=1";     public static final string newsurl2 = "http://tests.intellex.rs/api/v1/news/list?page=2";     public static final string newsurl3 = "http://tests.intellex.rs/api/v1/news/list?page=3";     public static final string newsurl4 = "http://tests.intellex.rs/api/v1/news/list?page=4";      private newsadapter adapter;     private recyclerview recyclerview;     private arraylist<newsmodel> newsarray;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.a_news_recycler_view__);          newsarray = new arraylist<>();         adapter = new newsadapter(this, newsarray);         recyclerview.layoutmanager mlayoutmanager = new linearlayoutmanager(getapplicationcontext());         recyclerview = (recyclerview) findviewbyid(r.id.newsrecyclerview);         recyclerview.setlayoutmanager(mlayoutmanager);         recyclerview.additemdecoration(new simpledivideritemdecoration(getapplicationcontext()));          recyclerview.setadapter(adapter);         recyclerview.sethasfixedsize(true);          newsasynctask task = new newsasynctask();         task.execute();     }     private class newsasynctask extends asynctask<url, void, arraylist<newsmodel>> {         @override         protected arraylist<newsmodel> doinbackground(url... urls) {              url url = createurl(newsurl1);             string jsonresponse = "";             try {                 jsonresponse = makehttprequest(url);              } catch (ioexception e) {                 e.printstacktrace();             }             return extractfeaturefromjson(jsonresponse);         }         @override        protected void onpostexecute(arraylist<newsmodel> news) {             if (news == null) {                 return;             }             adapter.addall(news);         }         private url createurl(string stringurl) {             url url = null;             try {                 url = new url(stringurl);             } catch (malformedurlexception exception) {                 log.e(log_tag, "error creating url", exception);                 return null;             }             return url;         }          private string makehttprequest(url url) throws ioexception {             string jsonresponse = "";             httpurlconnection urlconnection = null;             inputstream inputstream = null;             try {                 urlconnection = (httpurlconnection) url.openconnection();                 urlconnection.setrequestmethod("get");                 urlconnection.setreadtimeout(10000);                 urlconnection.setconnecttimeout(15000);                 urlconnection.connect();                  if (urlconnection.getresponsecode() == 200) {                     inputstream = urlconnection.getinputstream();                     jsonresponse = readfromstream(inputstream);                 } else {                     log.e(log_tag, "error response code: " + urlconnection.getresponsecode());                 }             } catch (ioexception e) {                 log.e(log_tag, "problem retrieving json results.", e);             } {                 if (urlconnection != null) {                     urlconnection.disconnect();                 }                 if (inputstream != null) {                     // function must handle java.io.ioexception here                     inputstream.close();                 }             }             return jsonresponse;         }          private string readfromstream(inputstream inputstream) throws ioexception {             stringbuilder output = new stringbuilder();             if (inputstream != null) {                 inputstreamreader inputstreamreader = new inputstreamreader(inputstream, charset.forname("utf-8"));                 bufferedreader reader = new bufferedreader(inputstreamreader);                 string line = reader.readline();                 while (line != null) {                     output.append(line);                     line = reader.readline();                 }             }             return output.tostring();         }          private arraylist<newsmodel> extractfeaturefromjson(string newsjson) {             if (textutils.isempty(newsjson)) {                 return null;             }             arraylist<newsmodel> news_information = new arraylist<>();             try {                 jsonobject basejsonresponse = new jsonobject(newsjson);                 jsonarray newsarray = basejsonresponse.getjsonarray("list");                 (int = 0; < newsarray.length(); i++) {                     jsonobject news = newsarray.getjsonobject(i);                     try {                         news = newsarray.getjsonobject(i);                         string newsimage = news.getstring("image");                         string newstitle = news.getstring("title");                         string newspublished = news.getstring("published");                         string newsauthor = news.getstring("author");                         string newsid = news.getstring("id");                          newsmodel newsmodel = new newsmodel(newsimage, newstitle, newspublished, newsauthor, newsid);                         news_information.add(newsmodel);                     } catch (jsonexception e) {                         e.printstacktrace();                     }                 }             } catch (jsonexception e) {                 e.printstacktrace();             }             return news_information;         }     } } 

any appreciated. in advance.

why don't use "links" array ? in case use array:

jsonobject jsonobject = new jsonobject(); jsonarray keys = jsonobject.getjsonarray("links"); int length = keys.length(); (int = 0; < length; i++) {     new readjson().execute(keys.getstring(i)); } 

anyway, take keys , go 1 after other, , query each edit:

jsonobject jsonobject = new jsonobject(/*your links json*/); jsonobject links = jsonobject.get("links"); iterator<string> keys = links.keys(); while (keys.hasnext()) {     new readjson().execute(links.getstring(keys.next())); } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -