Android Open Source - MobileConnectTestApp Json Utils






From Project

Back to project page MobileConnectTestApp.

License

The source code is released under:

MIT License

If you think the Android project MobileConnectTestApp 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.gsma.android.xoperatorapidemo.utils;
/*from w  w  w.ja v a 2s. co m*/
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.util.Log;

import com.gsma.android.xoperatorapidemo.discovery.DiscoveryData;
import com.gsma.android.xoperatorapidemo.discovery.Link;

/**
 * simple utilities which help with processing of JSON data
 */
public class JsonUtils {
  private static final String TAG = "JsonUtils";
  
  /**
   * convert JSON data to a Java object representing the JSON, uses the
   * in-built Android libraries
   * 
   * @param content
   * @param contentType
   * @return
   * @throws JSONException
   */
  public static Object convertContent(String content, String contentType)
      throws JSONException {
    String trimmed = content != null ? content.trim() : null;
    Object result = null;

    if (contentType != null
        && contentType.toLowerCase().startsWith("application/json")
        && trimmed != null && trimmed.length() > 0) {
      JSONTokener tokener = new JSONTokener(trimmed);
      result = tokener.nextValue();
    }
    return result;
  }

  /**
   * gets a named JSON String object from a root JSON object
   * 
   * @param object
   * @param identifier
   * @return
   */
  public static String getJSONStringElement(JSONObject object,
      String identifier) {
    String value = null;
    if (object != null) {
      try {
        value = object.getString(identifier);
      } catch (JSONException e) {
      }
    }
    return value;
  }

  /**
   * gets a named JSON object from a root JSON object
   * 
   * @param object
   * @param identifier
   * @return
   */
  public static JSONObject getJSONObject(JSONObject object, String identifier) {
    JSONObject value = null;
    if (object != null) {
      try {
        value = object.getJSONObject(identifier);
      } catch (JSONException e) {
      }
    }
    return value;
  }

  /**
   * gets a named JSON array from a root JSON object
   * 
   * @param object
   * @param identifier
   * @return
   */
  public static JSONArray getJSONArray(JSONObject object, String identifier) {
    JSONArray value = null;
    if (object != null) {
      try {
        value = object.getJSONArray(identifier);
      } catch (JSONException e) {
      }
    }
    return value;
  }

  /**
   * creates a simple error object from an error and error_description field
   * 
   * @param error
   * @param error_description
   * @return
   */
  public static JSONObject simpleError(String error, String error_description) {
    JSONObject container = new JSONObject();
    try {
      container.put("error", error);
    } catch (JSONException e) {
    }
    try {
      container.put("error_description", error_description);
    } catch (JSONException e) {
    }
    return container;
  }
  
  public static DiscoveryData readDiscoveryData(String strval) throws JSONException {
    DiscoveryData response=null;
    
    JSONObject json=new JSONObject(strval);
    response=new DiscoveryData(json);
    
    return response;
  }
  
  public static DiscoveryData readDiscoveryData(InputStream is) throws IOException, JSONException { 
    DiscoveryData response=null;
    StringBuffer buf=new StringBuffer();

    if (is!=null) {
      final Reader in = new InputStreamReader(is, "UTF-8");
      
      char[] b=new char[1024];
      int n;
      while ((n=in.read(b)) != -1) {
        buf.append(b, 0, n);
      }
      
      in.close();
      
      Log.d(TAG, "Parsing "+buf.toString());
      
      response=readDiscoveryData(buf.toString());
    }
    
    return response;
  }

  public static String readString(InputStream is) throws IOException, JSONException { 
    StringBuffer buf=new StringBuffer();

    if (is!=null) {
      final Reader in = new InputStreamReader(is, "UTF-8");
      
      char[] b=new char[1024];
      int n;
      while ((n=in.read(b)) != -1) {
        buf.append(b, 0, n);
      }
      
      in.close();
    }
    
    return buf.toString();
  }
  
  public static String getLinkArrayHref(Link[] link, String rel) {
    String href=null;
        if (link!=null && link.length>0 && rel!=null) {
          for (int i=0; i<link.length && href==null; i++) {
            if (rel.equalsIgnoreCase(link[i].getRel())) {
              href=link[i].getHref();
            }
          }
        }
        return href;
  }

}




Java Source Code List

com.gsma.android.xoperatorapidemo.activity.MainActivity.java
com.gsma.android.xoperatorapidemo.activity.SettingsActivity.java
com.gsma.android.xoperatorapidemo.activity.discovery.ActiveDiscoveryTask.java
com.gsma.android.xoperatorapidemo.activity.discovery.DiscoveryProcessEndpoints.java
com.gsma.android.xoperatorapidemo.activity.discovery.DisplayDiscoveryWebsiteActivity.java
com.gsma.android.xoperatorapidemo.activity.discovery.PassiveDiscoveryTask.java
com.gsma.android.xoperatorapidemo.activity.discovery.ProcessDiscoveryToken.java
com.gsma.android.xoperatorapidemo.activity.identity.AuthorizationCompleteActivity.java
com.gsma.android.xoperatorapidemo.activity.identity.DisplayIdentityWebsiteActivity.java
com.gsma.android.xoperatorapidemo.activity.identity.OpenIDConnectAbstractActivity.java
com.gsma.android.xoperatorapidemo.activity.identity.RetrieveTokenTask.java
com.gsma.android.xoperatorapidemo.activity.identity.RetrieveUserinfoTask.java
com.gsma.android.xoperatorapidemo.discovery.Api.java
com.gsma.android.xoperatorapidemo.discovery.DeveloperOperatorSetting.java
com.gsma.android.xoperatorapidemo.discovery.DiscoveryData.java
com.gsma.android.xoperatorapidemo.discovery.DiscoveryDeveloperOperatorSettings.java
com.gsma.android.xoperatorapidemo.discovery.DiscoveryServingOperatorSettings.java
com.gsma.android.xoperatorapidemo.discovery.DiscoveryStartupSettings.java
com.gsma.android.xoperatorapidemo.discovery.LinkConstants.java
com.gsma.android.xoperatorapidemo.discovery.Link.java
com.gsma.android.xoperatorapidemo.discovery.Response.java
com.gsma.android.xoperatorapidemo.discovery.ServingOperatorSetting.java
com.gsma.android.xoperatorapidemo.identity.UserinfoAddress.java
com.gsma.android.xoperatorapidemo.identity.Userinfo.java
com.gsma.android.xoperatorapidemo.logo.LogoCacheItem.java
com.gsma.android.xoperatorapidemo.logo.LogoCache.java
com.gsma.android.xoperatorapidemo.logo.LogoLoaderTask.java
com.gsma.android.xoperatorapidemo.logo.LogoResponseArray.java
com.gsma.android.xoperatorapidemo.logo.LogoResponse.java
com.gsma.android.xoperatorapidemo.utils.HttpUtils.java
com.gsma.android.xoperatorapidemo.utils.JsonUtils.java
com.gsma.android.xoperatorapidemo.utils.KeyValuePair.java
com.gsma.android.xoperatorapidemo.utils.ParameterList.java
com.gsma.android.xoperatorapidemo.utils.PhoneState.java
com.gsma.android.xoperatorapidemo.utils.PhoneUtils.java
com.gsma.android.xoperatorapidemo.utils.PreferencesUtils.java