Android Open Source - base_app J S O N Utils






From Project

Back to project page base_app.

License

The source code is released under:

GNU General Public License

If you think the Android project base_app 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.bengui.baseapp.utils;
//w  w  w. j  a v  a2 s  .com
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bengui.baseapp.services.ServiceException;

public abstract class JSONUtils {
  
  //Error attributes
  private static String TAG_ERR = "err";
  private static String TAG_ERR_NAME = "name";
  private static String TAG_ERR_MESSAGE = "message";
  
  /**
   * Validate if the response of the services are the JSON type
   * @param response Server response to validate
   * @throws ServiceException
   * @throws JSONException
   */
  public static void validateJsonObjetc(String response) throws ServiceException, JSONException {
    
    JSONObject jsonResponse;
    JSONArray jsonArray;
    
    try{
      jsonResponse = new JSONObject(response);
      
      if ( jsonResponse.has(TAG_ERR) ) {
        
        JSONObject err = (JSONObject) jsonResponse.getJSONObject(TAG_ERR);
        String nameError = err.getString(TAG_ERR_NAME);
        String messageError = err.getString(TAG_ERR_MESSAGE);
  
        throw new ServiceException(nameError, messageError);
        
      }
    }catch(JSONException e){
      
    
      jsonArray = new JSONArray(response);
      if ( jsonArray.getJSONObject(0).has(TAG_ERR) ) {
        
        JSONObject err = (JSONObject) jsonArray.getJSONObject(0).getJSONObject(TAG_ERR);
        String nameError = err.getString(TAG_ERR_NAME);
        String messageError = err.getString(TAG_ERR_MESSAGE);
        
        throw new ServiceException(nameError, messageError);
        
      }
      
    }
    
  }

  
  
}




Java Source Code List

com.bengui.baseapp.MainActivity.java
com.bengui.baseapp.gcm.GCMManager.java
com.bengui.baseapp.gcm.GcmBroadcastReceiver.java
com.bengui.baseapp.gcm.GcmIntentService.java
com.bengui.baseapp.models.Contact.java
com.bengui.baseapp.services.HttpClient.java
com.bengui.baseapp.services.ServerException.java
com.bengui.baseapp.services.ServiceException.java
com.bengui.baseapp.utils.Constants.java
com.bengui.baseapp.utils.JSONParcelableObject.java
com.bengui.baseapp.utils.JSONUtils.java
com.bengui.baseapp.utils.Utils.java