Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package dao; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import entity.Customer; import entity.Workshop; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; /** * * @author Joanne */ public class CustomerDAO { private final String USER_AGENT = "Mozilla/5.0"; public Customer getCustomer(int givenId) throws UnsupportedEncodingException, IOException { //Add URL here String url = ""; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); // add header post.setHeader("User-Agent", USER_AGENT); //Add parameters here List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("", "")); urlParameters.add(new BasicNameValuePair("", "")); post.setEntity(new UrlEncodedFormEntity(urlParameters)); HttpResponse response = client.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } String str = result.toString(); JsonParser jsonParser = new JsonParser(); JsonElement element = jsonParser.parse(str); JsonObject jobj = element.getAsJsonObject().getAsJsonObject("payload"); JsonElement attElement = jobj.get("id"); int id = attElement.getAsInt(); attElement = jobj.get("name"); String name = attElement.getAsString(); attElement = jobj.get("email"); String email = attElement.getAsString(); attElement = jobj.get("handphone"); String handphone = attElement.getAsString(); Customer customer = new Customer(id, name, email, handphone); return customer; } }