Android Open Source - android-json-http Parse 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;
//www .  j  a  v  a  2s  .  co m
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.google.gson.Gson;
import com.open.jsonhttp.ResponseBean;

/**
 * ??????
 * 
 * @author yinghui.hong
 */
public class ParseUtil {
  private static final String TAG = ParseUtil.class.getSimpleName();

  /**
   * ?HashMap???Query???
   * 
   * @param valueMap
   * @return
   */
  public static String parseMap2QueryStr(HashMap<String, Object> valueMap) {
    StringBuilder paramsStr = new StringBuilder();
    if (valueMap != null && valueMap.size() > 0) {
      paramsStr.append("?");
      for (String key : valueMap.keySet()) {
        paramsStr.append(key).append("=").append(valueMap.get(key))
            .append("&");
      }
      paramsStr.deleteCharAt(paramsStr.length() - 1);
    }
    return paramsStr.toString();

  }

  /**
   * ?HashMap???JSON????
   * 
   * @param valueMap
   *            ????????????????
   * @return String JSON???
   */
  public static String parseMap2JsonStr(HashMap<String, Object> valueMap) {
    Gson gson = new Gson();
    String jsonStr = gson.toJson(valueMap);
    Log.d(TAG, "????? --- " + jsonStr);
    return jsonStr;
  }

  /**
   * ???JSON???????
   * 
   * @param json
   *            ?????????????JSON???
   * @param classOfResponseBean
   *            ResponseBean????
   * @return ResponseBean?????
   * @throws JSONException
   */
  public static ResponseBean parseJson2Obj(String json,
      Class<? extends ResponseBean> classOfResponseBean)
      throws JSONException {
    Log.d(TAG, "?????? --- " + json);
    JSONObject jsonObject = new JSONObject(json);
    Gson gson = new Gson();
    ResponseBean bean = gson.fromJson(jsonObject.toString(),
        classOfResponseBean);
    return bean;
  }

}




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