Android Open Source - wizapp Rest A P I Host






From Project

Back to project page wizapp.

License

The source code is released under:

MIT License

If you think the Android project wizapp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.wb.wizapp.rest;
// w  w w. j ava 2 s.com
import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.wb.wizapp.IConstants;
import com.wb.wizapp.concurret.UIAsyncTask;
import com.wb.wizapp.concurret.UISyncTask;
import com.wb.wizapp.ui.BaseActivity;

public class RestAPIHost {
  protected final HttpHost host;
  protected final HttpClient httpClient;

  public RestAPIHost(final HttpHost host) {
    this(host, null);
  }

  public RestAPIHost(final HttpHost host, final String comstomiseUA) {
    this.host = host;

    httpClient = new DefaultHttpClient();
    HttpParams clientParams = httpClient.getParams();

    HttpClientParams.setRedirecting(clientParams, false);
    HttpConnectionParams.setConnectionTimeout(clientParams, 30000);
    HttpConnectionParams.setSoTimeout(clientParams, 30000);

    if (comstomiseUA != null) {
      final String customiseUserAgent = clientParams.getParameter(CoreProtocolPNames.USER_AGENT) + ";"
          + comstomiseUA;
      clientParams.setParameter(CoreProtocolPNames.USER_AGENT, customiseUserAgent);
    }
  }

  public String generateRequestUrl(final RestAPIService service) {
    return host.toURI() + service.getRequest().getRequestLine().getUri();
  }

  public void asyncCall(Context ctx, final RestAPIService service) {
    new UIAsyncTask<Void, Void, JSONObject>(ctx) {
      @Override
      protected JSONObject doInBackground(Void... params) {
        return RestAPIHost.this.call(service);
      }

      @Override
      protected void onPostExecute(JSONObject result) {
        Context ctx = wkCtx.get();
        if (ctx != null && service.builder != null) {
          service.builder.onPostResult(ctx, result);
        }
      };
    }.execute();
  }

  public void uiSyncCall(BaseActivity act, final RestAPIService service) {
    new UISyncTask<Void, Void, JSONObject>(act) {
      @Override
      protected JSONObject doInBackground(Void... params) {
        return RestAPIHost.this.call(service);
      }

      @Override
      protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);

        BaseActivity act = wkAct.get();
        if (act != null && service.builder != null) {
          service.builder.onPostResult(act, result);
        }
      };
    }.execute();
  }

  private JSONObject call(final RestAPIService service) {
    JSONObject rs = null;
    try {
      rs = httpClient.execute(host, service.getRequest(), service);
      Log.d(IConstants.LOG_TAG, "http request sent");
    } catch (Exception e) {
      Log.e(IConstants.LOG_TAG, "http request failed", e);
      if (service.builder != null) {
        service.builder.onExcpetion(e);
      }
    }
    return rs;
  }
}




Java Source Code List

com.wb.wizapp.IConstants.java
com.wb.wizapp.concurret.UIAsyncTask.java
com.wb.wizapp.concurret.UISyncTask.java
com.wb.wizapp.ex.WizError.java
com.wb.wizapp.rest.IJsonParsable.java
com.wb.wizapp.rest.IRestAPIServiceBuilder.java
com.wb.wizapp.rest.JsonBean.java
com.wb.wizapp.rest.JsonObject.java
com.wb.wizapp.rest.RestAPIHostFactory.java
com.wb.wizapp.rest.RestAPIHost.java
com.wb.wizapp.rest.RestAPISercieBuilderAdapter.java
com.wb.wizapp.rest.RestAPIService.java
com.wb.wizapp.router.RouteeCallback.java
com.wb.wizapp.router.Routee.java
com.wb.wizapp.router.Router.java
com.wb.wizapp.ui.BaseActivity.java
com.wb.wizapp.ui.BaseProgressFragment.java
com.wb.wizapp.ui.DetailActivity.java
com.wb.wizapp.ui.MainActivity.java
com.wb.wizapp.ui.MainDrawerFragment.java
com.wb.wizapp.ui.RouterWebViewActivity.java
com.wb.wizapp.ui.RouterWebView.java
com.wb.wizapp.ui.zxing.ScanActivity.java