rx java - is there a better way to retrieve this Json? -
i've made code download json:
private void rxpublishprogress() { final okhttpclient mokhttpclient = new okhttpclient(); final request mrequest = new request.builder().url(airportservice.service_endpoint).build(); observable.create(new observable.onsubscribe<string>() { @override public void call(subscriber<? super string> subscriber) { try { inputstream inputstream; okhttp3.response response = mokhttpclient.newcall(mrequest).execute(); if (response.issuccessful()) { inputstream = response.body().bytestream(); long len = response.body().contentlength(); string progress = "0"; subscriber.onnext(progress); bufferedreader reader = new bufferedreader(new inputstreamreader(inputstream, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); byte[] data = new byte[1024]; long total = 0; int count; string line = null; final int buffersize = 1024; boolean flag = false; jsonobject jsonobject = null; final char[] buffer = new char[buffersize]; final stringbuilder out = new stringbuilder(); reader in = new inputstreamreader(inputstream, "utf-8"); for(;flag ==false ;) { count = in.read(buffer, 0, buffer.length); if (count == -1) { progress = "100"; subscriber.onnext(progress); flag = true; } else { out.append(buffer, 0, count); // log.d("out",out.tostring()); total += count; progress = string.valueof(total * 100 / len); subscriber.onnext(progress); } } inputstream.close(); // try parse string json object try { jsonobject = new jsonobject(out.tostring()); } catch (jsonexception e) { e.printstacktrace(); } log.d("lengt",string.valueof(jsonobject.length())); subscriber.oncompleted(); } } catch (ioexception e) { e.printstacktrace(); } } }).subscribeon(schedulers.newthread()).subscribe(new subscriber<string>() { @override public void oncompleted() { log.d("complete","complete"); } @override public void onerror(throwable e) { } @override public void onnext(final string progress) { getactivity().runonuithread(new runnable() { @override public void run() { percentage.settext(progress+"%"); } }); } });
and works, don't know how retrieve json or convert gson.
i need retrieve percentage of download because it's specific request of project, need jsonobject or list . how have this?
thanks
as got have 1 observable
, need:
1) display percentage
2) else
so can this:
observable<okhttp3.response> responseobservable = observable.create(/*your code return response*/) .subscribeon(schedulers.newthread()) .replay(1) .refcount(); responseobservable .map(topercents()) .subscribe(displaypercents()); responseobservable .map(tosomethingelse()) .subscribe(displaysomethingelse());
Comments
Post a Comment