i have wcf webservice returns sql server database data in json format, i'm using asynctask data in android code :
public class fetchinfo extends asynctask { private string year = "94"; private string month = "1"; private string requested_column = ""; private string kodemelli = ""; public fetchinfo(string year, string month,string requested_column, string kodemelli) { this.year = year; this.month = month; this.requested_column = requested_column; this.kodemelli = kodemelli; } @override protected string doinbackground(object[] params) { try { system.out.println("guru"); defaulthttpclient httpclient=new defaulthttpclient(); //connect server httpget httpget=new httpget("http://serverip:8005/service1.svc/checklogin1?year="+year+"&month="+month+"&requested_column="+requested_column+"&kode_melli="+kodemelli); //get response string result = ""; httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); inputstream stream=httpentity.getcontent(); result = convertstreamtostring(stream); //convert stream readable format if (result.contains("\"error\"") || result.contains("\"server error\"")){ main.result = "0"; mainfish.result = "0"; }else { main.result = result; mainfish.result = result; } } catch (exception e) { main.result = "error"; mainfish.result = "error"; } return ""; } public static string convertstreamtostring(inputstream is) { bufferedreader reader = new bufferedreader(new inputstreamreader(is)); stringbuilder sb = new stringbuilder(); string line = null; try { while ((line = reader.readline()) != null) { sb.append(line + "\n"); } } catch (ioexception e) { e.printstacktrace(); } { try { is.close(); } catch (ioexception e) { e.printstacktrace(); } } return sb.tostring(); } } by ,when requests more example 10 request, response time long. topic how speed fetching of data server using json? realized should use retrofit instead asynctask make program work.
now question how can convert asynctask class retrofit ?
thanks.
ok first not use async task retrofit. retrofit takes care of async part. first need define rest adapter.
here default rest adapter parameters in it.
public class apiclient { private static apiinterface sapiinterface; public static apiinterface getapiclient(context context) { //build rest adapter if (sapiinterface == null) { final restadapter restadapter = new restadapter.builder() .setendpoint("http://serverip:8005") .build(); sapiinterface = restadapter.create(apiinterface.class); } return sapiinterface; } public interface apiinterface { // /service1.svc/checklogin1?year="+year+"&month="+month+"&requested_column="+requested_column+"&kode_melli="+kodemelli @get("/service1.svc/checklogin1") void getyourobject( @query("year") string year, @query("month") string month, @query("requested_column") string requested_column, @query("kode_melli") string kodemelli, retrofitcallback<yourobjectmodel> callback); } after have rest adapter need model put data in. use jsonschema2pojo convert valid json pojo model. here example of 1 of json responses , model.
{ "id": "37", "title": "", "short_name": "", "short_description": "", "full_description": "", "url": "http:\/\/\/shows\/programname", "image": { "url": "https:\/\/s3.amazonaws.com\/image\/original\/b2mwdpx.jpg", "url_sm": "https:\/\/s3.amazonaws.com\/image\/small\/b2mwdpx.jpg", }, "image": "b2mwdpx.jpg", "hosts": [{ "id": "651", "display_name": "", "username": "", "image": { "url": "https:\/\/s3.amazonaws.com\/image\/original\/selfie.jpg", "url_sm": "https:\/\/s3.amazonaws.com\/image\/small\/selfie.jpg", } }], "airtimes": [{ "start": "11:00:00", "end": "13:30:00", "weekday": "6" }], "categories": [{ "id": "84", "title": "bluegrass", "short_name": "bluegrass" }], "meta": [] } i model. , few others go it.
public class program { @expose private doublea.models.airtime airtime; @expose private string id; @expose private string title; @serializedname("short_name") @expose private string shortname; @serializedname("full_description") @expose private string fulldescription; @serializedname("short_description") @expose private string shortdescription; @expose private doublea.models.image image; @serializedname("image") @expose private string imagename; @expose private list<host> hosts = new arraylist<host>(); @expose private list<category> categories = new arraylist<category>(); @expose private list<airtime> airtimes = new arraylist<airtime>(); /** getters , setters */ public program() { } then make sure take care of failure cases.
public class retrofitcallback<s> implements callback<s> { private static final string tag = retrofitcallback.class.getsimplename(); @override public void success(s s, response response) { } @override public void failure(retrofiterror error) { log.e(tag, "failed make http request for: " + error.geturl()); response errorresponse = error.getresponse(); if (errorresponse != null) { log.e(tag, errorresponse.getreason()); if (errorresponse.getstatus() == 500) { log.e(tag, "handle server errors here"); } } } } and making api call.
private void executeprogramapicall(string year, string month, string requested_column, string kodemelli) { apiclient.getapiclient(this).getprogram(year, month, requested_column, kodemelli, new retrofitcallback<program>() { @override public void success(yourobjectmodel program, response response) { super.success(yourobjectmodel , response); //todo: here objectmadel } }); } i highly recommend taking @ retrofit documentation in case need special headers or request interceptors. happy coding.
Comments
Post a Comment