Package com.dragontek.mygpoclient.json

Source Code of com.dragontek.mygpoclient.json.JsonClient

package com.dragontek.mygpoclient.json;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.*;
import org.apache.http.client.methods.HttpUriRequest;

import com.dragontek.mygpoclient.http.HttpClient;
import com.google.gson.Gson;

public class JsonClient extends HttpClient {

  public JsonClient()
  {
    super();
  }
  public JsonClient(String username, String password)
  {
    super(username, password);
  }

  @Override
  protected HttpUriRequest prepareRequest(String method, String uri, HttpEntity data) throws UnsupportedEncodingException {
    HttpUriRequest request = super.prepareRequest(method, uri, data);
    request.addHeader("Accept", "application/json");
    return request;
  }

  @Override
  protected String processResponse(HttpResponse response) throws IllegalStateException, IOException {
    String data = (String) super.processResponse(response);
    //return decode(data);
    return data;
  };
 
  public static String encode(Object data)
  {
    Gson gson = new Gson();
    return gson.toJson(data);
  }
 
  public static <T> T decode(String data, Class<T> clazz)
  {
    Gson gson = new Gson();
    return gson.fromJson(data, clazz);
  }
}
TOP

Related Classes of com.dragontek.mygpoclient.json.JsonClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.