Android Open Source - gm-httpengine-studio G M Http Response






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;
// ww  w  .  jav a 2  s .c o m
import java.io.UnsupportedEncodingException;

import org.json.JSONException;
import org.json.JSONObject;

/***
 * Http response get from GMHttpEngine request
 * 
 * @author Gemini
 * 
 */
public class GMHttpResponse {

  private byte[] rawData;
    private int httpStatusCode;
  private HttpResponseParser<?> responseParser;
    private Exception exception;

  private boolean isFail = false;

    public void setRawData(byte[] data) {
        if (data != null) {
            this.rawData = data;
        } else {
            isFail = true;
        }
    }

  public byte[] getRawData() {
    this.filterException();
    return this.rawData;
  }

  public HttpResponseParser getResponseParser() {
        return responseParser;
  }

  public void setResponseParser(HttpResponseParser<?> responseParser) {
    this.responseParser = responseParser;
  }

  public String parseAsString() {
        return parseAsString("UTF-8");
  }

  public String parseAsString(String encode) {
    String ret = null;
        this.filterException();
    try {
      ret = new String(this.rawData, encode);
    } catch (UnsupportedEncodingException e) {
      isFail = true;
      throw new GMHttpException("Unsupport encoding " + e.getMessage());
    }
    return ret;
  }

    public void setHttpStatusCode(int code) {
        this.httpStatusCode = code;
        int status = code / 100;
        switch (status) {
        case 4: {
            // 400 code
            this.isFail = true;
            break;
        }
        case 5: {
            // 500 code
            this.isFail = true;
            break;
        }
        }
    }

    public int getHttpStatusCode() {
        return this.httpStatusCode;
    }

    public Exception getException() {
        return exception;
    }

    public void setException(Exception exception) {
        this.exception = exception;
        this.isFail = true;
    }

    public JSONObject parseAsJSON() throws JSONException{
    String result = parseAsString();
    JSONObject obj = new JSONObject(result);
    return obj;
  }

    public boolean isFail() {
        return this.isFail;
    }

  public Object parseData() {
        this.filterException();
    return responseParser.handleResponse(this.rawData);
  }

    private void filterException() throws RuntimeException{
        if (isFail) {
            throw new GMHttpException("Request is failed, with code: " + this.httpStatusCode);
        }
    }
}




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