Where to get a simple Android API Client?

123 views Asked by At

Many of us need to perform web service to get data from servers to be used in our Android apps. I have developed a simple android api client using RESTful API. the library is very easy to use and does simple Http request that support four Http methods (GET, POST, PUT, and DELETE).

With this library you can build a http request and handle it's response result which categories into five categories:

  1. Informational response.
  2. Successful response.
  3. Client error response.
  4. Server error response.
  5. Exception response.

in the answer there is a simple example on how to use the library.

1

There are 1 answers

0
moh.sukhni On BEST ANSWER

Here is the link for the library on GitHub

https://github.com/jorcad/AndroidApiClient

you can import the jar (android-api-client.jar) file to your project from the bin folder.

with this library you can specifiy the following parameter:

  • Set the base Uri of the call.
  • Set Http Method.
  • add paths, parameters, and headers.
  • Set Content type.
  • Set entity/content/body.
  • Set connection and socket timeout.
  • Set connection and socket timeout retry count.
  • Set whether to enable retry when connection or socket timeout happens.
  • execute the request in three ways: Block, Thread, or Async task.
  • Set Handlers to get result for different response codes: Informational, Successful, ClientError, and ServerError.
  • Get response entity ( response code,content, content length, and headers).

Example on how to build your first Api Call and handle the 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", "[email protected]")
    .addHeader("ContentType", "application/xml")
    .setTextContent("<Root><Test>this is test</Test></Root>")
    .setMethod(Method.POST)
    .setConnectionTimeout(6000)
    .setSocketTimeout(6000)
    .build();

    //execute and handle the 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 as 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 as 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 as 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 as 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 as string: " + entity.getResponseContentAsString());

        }

        @Override
        public void onException(ExceptionStatus exceptionStatus, Exception e)
        {
            Log.e("ApiClient", "Exception code: " + exceptionStatus.code());
            Log.e("ApiClient", "Exception: " + e.getMessage());
        }

    });