...
Sen jälkeen koodia voi ajaa myös suoraan Eclipsestä valitsemalla Run As -> Java Applicationopendata-reservation.zip
Java-esimerkki
Code Block | ||
---|---|---|
| ||
package com.example; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonSyntaxException; public class Main { private static String API_KEY = "oma_api_key"; public static void main(String[] a) throws Exception { ReservationService service = new ReservationService(); System.out.println(service.getReservation("1234")); JReservationQuery query = new JReservationQuery(); DateFormat df = new SimpleDateFormat("dd.MM.yyyy"); query.startDate = df.parse("15.1.2014"); query.endDate = df.parse("17.1.2014"); query.room = Arrays.asList("G205"); System.out.println(service.searchReservations(query)); } public static class ReservationService { private String serviceUrl = "http://opendata.metropolia.fi/r1/reservation"; private static Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd'T'HH:mm").disableHtmlEscaping().create(); public JReservation getReservation(String id) throws JsonSyntaxException, ParseException, IOException { HttpClient client = getClient(); HttpGet get = new HttpGet(serviceUrl + "/" + id + "?apiKey=" + API_KEY); HttpResponse response = client.execute(get); try { JResult result = gson.fromJson( EntityUtils.toString(response.getEntity(), "UTF-8"), JResult.class); if (!result.reservations.isEmpty()) { return result.reservations.get(0); } return null; } finally { get.releaseConnection(); } } public List<JReservation> searchReservations(JReservationQuery query) throws ClientProtocolException, IOException { HttpClient client = getClient(); HttpPost post = new HttpPost(serviceUrl + "/search?apiKey=" + API_KEY); String json = gson.toJson(query); StringEntity entity = new StringEntity(json, ContentType.create( "application/json", "UTF-8")); post.setEntity(entity); HttpResponse response = client.execute(post); try { JResult result = gson.fromJson( EntityUtils.toString(response.getEntity(), "UTF-8"), JResult.class); return result.reservations; } finally { post.releaseConnection(); } } private HttpClient getClient() { DefaultHttpClient client = new DefaultHttpClient(); HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, 30000); HttpConnectionParams.setSoTimeout(params, 30000); return client; } } public static class JResult { public String status; public List<JReservation> reservations; } public static class JReservation { public String id; public String subject; public Date startDate; public Date endDate; public List<JResource> resources; public String toString() { return "\n" + subject + " " + startDate + " - " + endDate + " (" + resources + ")"; } } public static class JResource { public String id; public String type; public String code; public String name; public String toString() { return name+" "+code; } } public static class JReservationQuery { public Date startDate; public Date endDate; public List<String> room; } } |