Android Open Source - gm-httpengine-studio Form Url Encoded 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;
/*from www.  j  a  v  a2  s .  c o m*/
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Set;

public class FormUrlEncodedParser implements HttpRequestParser {

    private String mEncodingString = "UTF-8";

  /***
   * parse form body
   */
  @Override
  public byte[] parse(GMHttpParameters httpParameters) throws IOException,GMHttpException {
        if(httpParameters == null) {
            return null;
        }
        Set<String> keySet = httpParameters.getNames();
        ArrayList<NameValuePair> nvps = new ArrayList<NameValuePair>();
        for (String name : keySet) {
            Object value = httpParameters.getParameter(name);
            if (!(value instanceof File)) {
                if (value == null) {
                    continue;
                }
                NameValuePair p = new BasicNameValuePair(name, value.toString());
                nvps.add(p);
            } else {
                throw new GMHttpException("FormUrlEncoding cannot have file part");
            }
        }
        HttpEntity entity = new UrlEncodedFormEntity(nvps, mEncodingString);
        InputStream is = entity.getContent();
        int available = is.available();
        byte[] buffer = new byte[available];
        is.read(buffer);
        is.close();
        return buffer;
  }


  @Override
  public String getContentType() {
        return "application/x-www-form-urlencoded; charset=" + mEncodingString;
  }

    @Override
    public void setEncoding(String encoding) {
        this.mEncodingString = encoding;
    }

    @Override
    public long getContentLength() {
        return -1;
    }
}




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