java - Where to get a simple android API Client? -
many of need perform web service data servers used in our android apps. have developed simple android api client using restful api. library easy use , simple http request support 4 http methods (get, post, put, , delete).
with library can build http request , handle it's response result categories 5 categories:
- informational response.
- successful response.
- client error response.
- server error response.
- exception response.
in answer there simple example on how use library.
thanks.
here link library on github
https://github.com/jorcad/androidapiclient
you can import jar (android-api-client.jar) file project bin folder.
with library can specifiy following parameter:
- set base uri of call.
- set http method.
- add paths, parameters, , headers.
- set content type.
- set entity/content/body.
- set connection , socket timeout.
- set connection , socket timeout retry count.
- set whether enable retry when connection or socket timeout happens.
- execute request in 3 ways: block, thread, or async task.
- set handlers result different response codes: informational, successful, clienterror, , servererror.
- get response entity ( response code,content, content length, , headers).
example on how build first api call , handle result:
apiclient apiclient = new apiclientbuilder() .setbaseuri("http://localhost:8181") .addpath("webservices") .addpath("rest") .addpath("accout") .addpath("create") .addparam("username", "user123") .addparam("firstname", "mike") .addparam("lastname", "norm") .addparam("password", "p@ssw0rd") .addparam("email", "mike@test.com") .addheader("contenttype", "application/xml") .settextcontent("<root><test>this test</test></root>") .setmethod(method.post) .setconnectiontimeout(6000) .setsockettimeout(6000) .build(); //execute , handle result apiclient.executeonasynctask(new apiclienthandler() { @override public void oninformational(status status, string responsestatus, responseentity entity) { log.i("apiclient", "response content code: " + status.code()); log.i("apiclient", "response content status: " + responsestatus); log.i("apiclient", "response content lenght: " + entity.getcontentlength()); log.i("apiclient:", "response content string: " + entity.getresponsecontentasstring()); } @override public void onsuccessful(status status, string responsestatus, responseentity entity) { log.v("apiclient", "response content code: " + status.code()); log.v("apiclient", "response content status: " + responsestatus); log.v("apiclient", "response content lenght: " + entity.getcontentlength()); log.v("apiclient:", "response content string: " + entity.getresponsecontentasstring()); } @override public void onclienterror(status status, string responsestatus, responseentity entity) { log.v("apiclient", "response content code: " + status.code()); log.v("apiclient", "response content status: " + responsestatus); log.v("apiclient", "response content lenght: " + entity.getcontentlength()); log.v("apiclient:", "response content string: " + entity.getresponsecontentasstring()); } @override public void onservererror(status status, string responsestatus, responseentity entity) { log.e("apiclient", "response content code: " + status.code()); log.e("apiclient", "response content status: " + responsestatus); log.e("apiclient", "response content lenght: " + entity.getcontentlength()); log.e("apiclient:", "response content string: " + entity.getresponsecontentasstring()); } @override public void onredirection(status status, string responsestatus, responseentity entity) { log.i("apiclient", "response content code: " + status.code()); log.i("apiclient", "response content status: " + responsestatus); log.i("apiclient", "response content lenght: " + entity.getcontentlength()); log.i("apiclient:", "response content string: " + entity.getresponsecontentasstring()); } @override public void onexception(exceptionstatus exceptionstatus, exception e) { log.e("apiclient", "exception code: " + exceptionstatus.code()); log.e("apiclient", "exception: " + e.getmessage()); } });
Comments
Post a Comment