Android Open Source - gm-httpengine-studio G M Model Parser






From Project

Back to project page gm-httpengine-studio.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project gm-httpengine-studio 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 org.gemini.httpengine.library;
// www .j  av a 2  s  . co  m
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by geminiwen on 14-3-31.
 *
 * Convert a simple object to  {@link org.gemini.httpengine.library.GMHttpParameters}
 *
 */
public class GMModelParser {

    public GMHttpParameters parseModel(Object httpParamsModel) {
        GMHttpParameters httpParameters = new GMHttpParameters();
        Field[] fields = httpParamsModel.getClass().getDeclaredFields();
        for (Field field : fields) {

            if (field.isAnnotationPresent(HttpParameter.class)) {
                HttpParameter parameter = field.getAnnotation(HttpParameter.class);
                String parameterName = parameter.name();
                try {
                    String fieldName = getGenericFieldName(field.getName());
                    Method getterMethod = null;
                    StringBuilder getterBuilder = new StringBuilder("");
                    String getterMethodName;

                    if (field.getType().equals(boolean.class)) {
                        getterBuilder.append("is");
                    } else {
                        getterBuilder.append("get");
                    }
                    getterBuilder.append(fieldName);
                    getterMethodName = getterBuilder.toString();
                    getterMethod = httpParamsModel.getClass().getMethod(getterMethodName);

                    Object value = getterMethod.invoke(httpParamsModel);
                    if (null != value) {
                        if(!(value instanceof File)) {
                            httpParameters.setParameter(parameterName, value.toString());
                        } else {
                            httpParameters.setParameter(parameterName, (File)value);
                        }
                    }

                } catch (NoSuchMethodException e) {
                    String value = getValueByField(field, httpParamsModel);
                    if(null != value) {
                        httpParameters.setParameter(parameterName, value);
                    }
                } catch (IllegalAccessException e) {
                    String value = getValueByField(field, httpParamsModel);
                    if(null != value) {
                        httpParameters.setParameter(parameterName, value);
                    }
                } catch (InvocationTargetException e) {
                    // Never in this block
                }
            }

        }
        return httpParameters;
    }

    /***
     * get field name. modelName => ModelName
     * @param fieldName modelName
     * @return  ModelName
     */
    private String getGenericFieldName(String fieldName) {
        String newStr = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        return newStr;
    }

    private String getValueByField(Field field,Object model) {
        Object value = null;
        try {
            boolean accessible = field.isAccessible();
            field.setAccessible(true);
            value = field.get(model);
            field.setAccessible(accessible);
        } catch (IllegalAccessException ex) {
            // Never enter this block
        }
        return value.toString();
    }

}




Java Source Code List

org.gemini.httpengine.examples.LoginAPI.java
org.gemini.httpengine.examples.MainActivity.java
org.gemini.httpengine.library.Config.java
org.gemini.httpengine.library.FormUrlEncodedParser.java
org.gemini.httpengine.library.GMHttpEngine.java
org.gemini.httpengine.library.GMHttpException.java
org.gemini.httpengine.library.GMHttpLoader.java
org.gemini.httpengine.library.GMHttpMethodExecutor.java
org.gemini.httpengine.library.GMHttpParameters.java
org.gemini.httpengine.library.GMHttpRequest.java
org.gemini.httpengine.library.GMHttpResponse.java
org.gemini.httpengine.library.GMHttpResult.java
org.gemini.httpengine.library.GMHttpService.java
org.gemini.httpengine.library.GMModelParser.java
org.gemini.httpengine.library.GMTrustManager.java
org.gemini.httpengine.library.HttpMethod.java
org.gemini.httpengine.library.HttpParameter.java
org.gemini.httpengine.library.HttpRequestParser.java
org.gemini.httpengine.library.HttpResponseParser.java
org.gemini.httpengine.library.LOG.java
org.gemini.httpengine.library.MultiPartParser.java
org.gemini.httpengine.library.MultipartEntity.java
org.gemini.httpengine.library.OnProgressUpdateListener.java
org.gemini.httpengine.library.OnResponseListener.java