Android Open Source - Wardrobe_app Server Response






From Project

Back to project page Wardrobe_app.

License

The source code is released under:

Apache License

If you think the Android project Wardrobe_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.android.busolo.apps.wardrobe.utils;
/*ww w  .j  a v a2 s.c om*/
/**
 * Created by s-brian on 5/24/2014.
 */
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import static com.android.busolo.apps.wardrobe.utils.LogUtils.LOGE;

/**
 * Base Class for classes which provide data returned from the server.
 */
public class ServerResponse {
    /**
     * The parameter for a null setter
     */

    private static final Object[] NULL_SETTER_PARAMETERS = { null };

    /**
     * The setter map for this class.
     */

    private Map<String, Method> mSetterMap = null;

    /**
     * Set the properties in this object using the data in the JSON representation.
     *
     * @param jsonObject The object to get the property data from.
     */

    public void fromJSON(final JSONObject jsonObject)
            throws JSONException {
        try {
            setPropertiesByReflection(jsonObject);
        } catch (InvocationTargetException e) {
            LOGE("", "Problem parsing JSON response", e);
            throw new JSONException("Problem parsing response");
        } catch (IllegalAccessException e) {
            LOGE("", "Problem parsing JSON response", e);
            throw new JSONException("Problem parsing response");
        }
    }

    /**
     * Set all of the properties in this object using reflection. If a property in
     * the JSON object isn't found in the object it's ignored, this allows older
     * client to remain compatible with newer server responses.
     *
     * @param jsonObject The JSON object to extract the data from.
     */

    void setPropertiesByReflection(final JSONObject jsonObject)
            throws IllegalAccessException, InvocationTargetException, JSONException {

        // Build a map of the setters in this object if it doesn't exist.
        synchronized (this) {
            if(mSetterMap == null) {
                mSetterMap = new HashMap<String, Method>();
                Method[] methods = getClass().getMethods();
                for(Method method : methods) {
                    final String methodName = method.getName();
                    // Skip methods with more than one parameter
                    if(method.getParameterTypes().length != 1) {
                        continue;
                    }
                    // Skip non-setters
                    if(!methodName.startsWith("set")) {
                        continue;
                    }

                    // Change setBlah to be blah to reflect the attribute name in the JSON representation
                    mSetterMap.put(Character.toLowerCase(methodName.charAt(3))+methodName.substring(4), method);
                }
            }
        }


        // JSONObject is backed
        Iterator keys = jsonObject.keys();
        while(keys.hasNext()) {
            String key = keys.next().toString();
            if(!mSetterMap.containsKey(key)) {
                continue;
            }

            Method method = mSetterMap.get(key);

            if      (jsonObject.isNull(key)) {
                method.invoke(this, NULL_SETTER_PARAMETERS);
                continue;
            }

            Class setterType = method.getParameterTypes()[0];

            if          (setterType.equals(String.class)) {
                method.invoke(this, jsonObject.getString(key));
            } else if   (setterType.equals(Double.class)) {
                method.invoke(this, jsonObject.getDouble(key));
            } else if   (setterType.equals(Long.class)) {
                method.invoke(this, jsonObject.getLong(key));
            } else if   (setterType.equals(Boolean.class)) {
                method.invoke(this, jsonObject.getBoolean(key));
            } else if   (setterType.equals(Integer.class)) {
                method.invoke(this, jsonObject.getInt(key));
            } else if   (setterType.equals(JSONArray.class)) {
                method.invoke(this, jsonObject.getJSONArray(key));
            } else if   (setterType.equals(JSONObject.class)) {
                method.invoke(this, jsonObject.getJSONObject(key));
            } else {
                if(!useClassSpecificSetter(jsonObject, key)) {
                    Log.w("Server Response", "Unable to deal with setter for "+key+" of type "+
                            jsonObject.get(key).getClass().getCanonicalName()+" in "+getClass().getCanonicalName());
                }
            }
        }
    }

    /**
     * Allows class specific overrides of attributes which are not detected by the default handler
     *
     * @param jsonObject The object containing the data.
     * @param key They key in the object which holds the data.
     */

    protected boolean useClassSpecificSetter(JSONObject jsonObject, final String key) throws JSONException {
        return false;
    }
}




Java Source Code List

com.android.busolo.apps.wardrobe.Config.java
com.android.busolo.apps.wardrobe.engine.BaseActivity.java
com.android.busolo.apps.wardrobe.engine.ColorPickerFragment.java
com.android.busolo.apps.wardrobe.engine.CommentsFragment.java
com.android.busolo.apps.wardrobe.engine.FollowActivity.java
com.android.busolo.apps.wardrobe.engine.HomeActivity.java
com.android.busolo.apps.wardrobe.engine.ItemDetailsActivity.java
com.android.busolo.apps.wardrobe.engine.LoginFragment.java
com.android.busolo.apps.wardrobe.engine.MatchFragment.java
com.android.busolo.apps.wardrobe.engine.NewPostActivity.java
com.android.busolo.apps.wardrobe.engine.PrivateStreamFragment.java
com.android.busolo.apps.wardrobe.engine.ProfileFragment.java
com.android.busolo.apps.wardrobe.engine.PublicStreamActivity.java
com.android.busolo.apps.wardrobe.engine.PublicStreamFragment.java
com.android.busolo.apps.wardrobe.engine.SignupFragment.java
com.android.busolo.apps.wardrobe.engine.StepOneFragment.java
com.android.busolo.apps.wardrobe.engine.StepTwoFragment.java
com.android.busolo.apps.wardrobe.engine.UserAccountActivity.java
com.android.busolo.apps.wardrobe.engine.adapter.ColorListAdapter.java
com.android.busolo.apps.wardrobe.engine.adapter.ColorSpinnerAdapter.java
com.android.busolo.apps.wardrobe.engine.adapter.FeedsListAdapter.java
com.android.busolo.apps.wardrobe.engine.adapter.GridViewPhotoAdapter.java
com.android.busolo.apps.wardrobe.engine.adapter.ViewInflaterBaseAdapter.java
com.android.busolo.apps.wardrobe.engine.model.ColorPicker.java
com.android.busolo.apps.wardrobe.engine.model.FilterParam.java
com.android.busolo.apps.wardrobe.engine.model.Follow.java
com.android.busolo.apps.wardrobe.engine.model.Stream.java
com.android.busolo.apps.wardrobe.sync.SyncHelper.java
com.android.busolo.apps.wardrobe.sync.SyncService.java
com.android.busolo.apps.wardrobe.utils.AccountUtils.java
com.android.busolo.apps.wardrobe.utils.BitmapCache.java
com.android.busolo.apps.wardrobe.utils.ImageLoader.java
com.android.busolo.apps.wardrobe.utils.LogUtils.java
com.android.busolo.apps.wardrobe.utils.LruBitmapCache.java
com.android.busolo.apps.wardrobe.utils.NetUtils.java
com.android.busolo.apps.wardrobe.utils.ParserUtils.java
com.android.busolo.apps.wardrobe.utils.PrefUtils.java
com.android.busolo.apps.wardrobe.utils.ServerResponse.java
com.android.busolo.apps.wardrobe.utils.UIUtils.java
com.android.busolo.apps.wardrobe.utils.VolleyAppController.java
com.android.busolo.apps.wardrobe.utils.model.FeedResult.java
com.android.busolo.apps.wardrobe.widget.BezelImageView.java
com.android.busolo.apps.wardrobe.widget.CheckableFrameLayout.java
com.android.busolo.apps.wardrobe.widget.EllipsizedTextView.java
com.android.busolo.apps.wardrobe.widget.ObservableScrollView.java
com.android.busolo.apps.wardrobe.widget.SquareImageView.java