android - how to insert a percentage to download this Json -
i've use code download json file , convert project. since there 40k items download need add % of download of file. code is:
final retrofit retrofit = new retrofit.builder() .addconverterfactory(gsonconverterfactory.create()) .addcalladapterfactory(rxjavacalladapterfactory.create()) .baseurl(airportservice.service_endpoint).build(); airportservice service = retrofit.create(airportservice.class); service.getairport() .subscribeon(schedulers.newthread()) .observeon(androidschedulers.mainthread()) .subscribe(new subscriber<list<airport>>() { @override public void oncompleted() { } @override public void onerror(throwable e) { e.printstacktrace(); } @override public void onnext(list<airport> airports) { airps = airports; } });
but i'm @ beginning of retrofit , don't know it. me? thanks
here's code used accomplish similar. in example, had post json, download , parse json response , store response in sqlite database. need adapt code needs, concept simple. solution boils down to:
1) parse json using jsonreader.
2) use filterinputstream publish progress.
i recognize solution doesn't use retrofit, maybe won't want use it, perhaps find helpful.
final progressdialog dialog = new progressdialog(getactivity(), progressdialog.theme_holo_light); dialog.settitle(r.string.downloading_data); dialog.setmessage("downloading data..."); dialog.setcanceledontouchoutside(false); dialog.setprogressstyle(progressdialog.style_horizontal); if (!buildconfig.debug) { dialog.setprogressnumberformat(null); } mlogintask = new asynctask<void, integer, void>() { @override protected void doinbackground(void... params) { httpurlconnection connection = null; try { byte[] request = ...; // used jsonwriter write bytearrayoutputstream. since you're getting file or request, not necessary. url url = new url("https://example.com/endpoint"); connection = (httpurlconnection)url.openconnection(); connection.setdooutput(true); connection.setusecaches(false); connection.setrequestmethod("post"); connection.setrequestproperty("content-type", "application/json; charset=utf-8"); connection.setrequestproperty("content-length", "" + request.length); connection.setrequestproperty("accept","*/*"); outputstream out = connection.getoutputstream(); out.write(request); out.flush(); out.close(); connection.setconnecttimeout(3000); connection.setreadtimeout(10000); connection.connect(); thread.sleep(2000); // don't remember why necessary. exception thrown in next line without it, @ least on slower networks. int responsecode = connection.getresponsecode(); switch (responsecode) { case 200: int length = connection.getcontentlength(); publishprogress(0, length); final inputstreamreader streamreader = new inputstreamreader(new filterinputstream(connection.getinputstream()) { private int mtotalbytesread; @override public int read() throws ioexception { int b = super.read(); if (b > 0) { updateprogress(1); } return b; } @override public int read(byte[] buffer) throws ioexception { return updateprogress(super.read(buffer)); } @override public int read(byte[] buffer, int byteoffset, int bytecount) throws ioexception { return updateprogress(super.read(buffer, byteoffset, bytecount)); } @override public long skip(long bytecount) throws ioexception { return updateprogress(super.skip(bytecount)); } @override public void mark(int readlimit) { throw new unsupportedoperationexception(); } @override public void reset() throws ioexception { throw new unsupportedoperationexception(); } @override public boolean marksupported() { return false; } private int updateprogress(long numbytesread) { if (numbytesread > 0) { publishprogress(mtotalbytesread += numbytesread); } return (int)numbytesread; } }); jsonreader reader = new jsonreader(streamreader); // code here depend on format of code. seems have array. reader.beginarray(); while (reader.peek() == jsontoken.begin_object) { jsonobject airport = (jsonobject) streams.parse(reader); // stuff airport. perhaps create airport object , add arraylist. } return null; default: string response = ioutils.tostring(connection.geterrorstream()); try { util.showtoast(jsonutil.getstring(new jsonobject(response), constants.error), toast.length_long); if (responsecode == 403) { // forbidden publishprogress(); } } catch (exception e) { util.showtoast("unsupported response\n" + connection.getheaderfield(null)+"\n"+response, toast.length_long); } break; } } catch (unknownhostexception e) { util.showtoast("you don't appear connected internet.", toast.length_long); } catch (exception e) { if (e instanceof interruptedioexception) { return null; } util.showtoast(e.tostring(), toast.length_long); log.e("dashboardactivity", "error downloading data", e); } { if (connection != null) { connection.disconnect(); } } return null; } @override protected void onpostexecute(void result) { dialog.cancel(); taskutil.synchronize(homefragment.this); } @override protected void oncancelled() { dialog.cancel(); // todo: maybe have more cleanup } @override protected void onprogressupdate(integer... values) { if (values.length == 0) { myactivity.getcurrentactivity().showlogininfo(true); } else { dialog.setprogress(values[0]); if (values.length == 2) { dialog.setmax(values[1]); dialog.setindeterminate(false); dialog.show(); } } } }.executeonexecutor(asynctask.thread_pool_executor); dialog.setoncancellistener(new dialoginterface.oncancellistener() { @override public void oncancel(dialoginterface dialog) { mlogintask.cancel(true); } });
Comments
Post a Comment