Android Open Source - android-json-http Request Util






From Project

Back to project page android-json-http.

License

The source code is released under:

Apache License

If you think the Android project android-json-http 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.open.jsonhttp.util;
//ww  w.  j ava  2 s  . c o  m
import org.json.JSONException;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.open.jsonhttp.AsyncTask;
import com.open.jsonhttp.HttpResCode;
import com.open.jsonhttp.OnRequestHttpListener;
import com.open.jsonhttp.RequestBean;
import com.open.jsonhttp.RequestBeanFile;
import com.open.jsonhttp.ResponseBean;
import com.open.jsonhttp.WebServiceListener;

/**
 * ???????
 * 
 * @author yinghui.hong
 */
public class RequestUtil {

  private static final String TAG = RequestUtil.class.getSimpleName();

  /**
   * @param url
   * @param requestBean
   * @param filePath
   * @param classOfResponseBean
   * @param webServiceListener
   * @return
   */
  private static AsyncTask request(String url, RequestBean requestBean,
      final Class<? extends ResponseBean> classOfResponseBean,
      final WebServiceListener webServiceListener) {
    if (NetwrokUtil.isConnected()) {
      // HandlerThread handlerThread = new
      // HandlerThread("Handler Thread");
      // handlerThread.start();
      // Handler handler = new Handler(handlerThread.getLooper()) {
      Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
          super.handleMessage(msg);

          ResponseBean bean = null;
          if (msg.arg1 == HttpResCode.SUCCESS) {
            try {
              bean = ParseUtil.parseJson2Obj((String) msg.obj,
                  classOfResponseBean);
            } catch (JSONException e) {
              webServiceListener.onResponse(
                  HttpResCode.PARSE_JSON_ERROR, bean);
            }
          }
          webServiceListener.onResponse(msg.arg1, bean);
          Log.e("RequestUtil.java", "???? = " + msg.arg1);
        }
      };
      if (requestBean instanceof RequestBeanFile) {
        // ????
        url += ParseUtil.parseMap2QueryStr(requestBean.generateMap());
        return ThreadUtil.doPost(url, handler,
            ((RequestBeanFile) requestBean).generateFile());
      } else {
        // ???https://developers.google.com/maps/documentation/geocoding/?hl=zh-CN
        // JSON???
        // return
        // ThreadUtil.doGet("http://maps.googleapis.com/maps/api/geocode/json?address=HK&sensor=true",
        // handler);
        // return
        // ThreadUtil.doGet(url+"?access_token=2.00xt1dIC0ncBIQ67e61cb6cbJNqHcD",
        // handler);
        // return ThreadUtil.doPost(url, handler,
        // ParseUtil.parseMap2JsonStr(requestBean.generateMap()));
        url += ParseUtil.parseMap2QueryStr(requestBean.generateMap());
        return ThreadUtil.doGet(url, handler);
      }

    } else {
      Log.i(TAG,
          "***********************network is error *************************\n");
      webServiceListener.onResponse(HttpResCode.NETWORK_ERROR, null);
      return null;
    }
  }

  /**
   * @param onRequesthttplistener
   * @param index
   *            ?????????
   * @param url
   * @param requestBean
   * @param filePath
   *            if not null post the file ,others post the JSON string.
   * @param classOfResponseBean
   * @return
   */
  public static AsyncTask request(
      final OnRequestHttpListener onRequesthttplistener, final int index,
      String url, RequestBean requestBean,
      final Class<? extends ResponseBean> classOfResponseBean) {
    final WebServiceListener webServiceListener = new WebServiceListener() {
      @Override
      public void onResponse(int httpResCode, ResponseBean responseBean) {
        switch (httpResCode) {
        case HttpResCode.SUCCESS:
          onRequesthttplistener.onSuccess(index, responseBean);
          break;
        case HttpResCode.NETWORK_ERROR:
          onRequesthttplistener.onFail(index,
              HttpResCode.NETWORK_ERROR);
          break;
        case HttpResCode.TIME_OUT:
          onRequesthttplistener.onFail(index, HttpResCode.TIME_OUT);
          break;
        case HttpResCode.SERVER_ERROR:
          onRequesthttplistener.onFail(index,
              HttpResCode.SERVER_ERROR);
          break;
        case HttpResCode.PARSE_JSON_ERROR:
          onRequesthttplistener.onFail(index,
              HttpResCode.PARSE_JSON_ERROR);
          break;
        default:
          onRequesthttplistener.onFail(index,
              HttpResCode.SERVER_ERROR);
          break;
        }
      }
    };
    return request(url, requestBean, classOfResponseBean,
        webServiceListener);
  }
}




Java Source Code List

com.example.App.java
com.example.MainActivity.java
com.example.constant.Request.java
com.example.constant.Result.java
com.example.constant.Url.java
com.example.request.GeocodeReqBean.java
com.example.response.GeocodeResBean.java
com.example.response.table.AddressComponent.java
com.example.response.table.GeocodeTable.java
com.example.response.table.Geometry.java
com.example.response.table.LocationRectangle.java
com.example.response.table.Location.java
com.example.util.AccessApiUtil.java
com.open.jsonhttp.AsyncTask.java
com.open.jsonhttp.HttpRequest.java
com.open.jsonhttp.HttpResCode.java
com.open.jsonhttp.OnRequestHttpListener.java
com.open.jsonhttp.RequestBeanFile.java
com.open.jsonhttp.RequestBean.java
com.open.jsonhttp.ResponseBean.java
com.open.jsonhttp.WebServiceListener.java
com.open.jsonhttp.util.NetwrokUtil.java
com.open.jsonhttp.util.ParseUtil.java
com.open.jsonhttp.util.RequestUtil.java
com.open.jsonhttp.util.StringUtil.java
com.open.jsonhttp.util.ThreadUtil.java