Created
November 15, 2013 14:57
-
-
Save scottt/7485575 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tw.plate; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import com.google.gson.JsonDeserializationContext; | |
import com.google.gson.JsonDeserializer; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonParseException; | |
import java.lang.reflect.Type; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.Locale; | |
import retrofit.Callback; | |
import retrofit.RestAdapter; | |
import retrofit.client.Response; | |
import retrofit.converter.GsonConverter; | |
import retrofit.http.Field; | |
import retrofit.http.FormUrlEncoded; | |
import retrofit.http.GET; | |
import retrofit.http.POST; | |
import retrofit.http.Query; | |
public class PlateAPI { | |
public class Recommendation { | |
public String name; | |
public String pic_uri; | |
public int price; | |
public String restaurant_name; | |
} | |
public class RecommendationResponse { | |
public int success; | |
public List<Recommendation> list; | |
} | |
public class Restaurant { | |
public int location; | |
public String name; | |
public int rest_id; | |
} | |
public class RestaurantResponse { | |
public int success; | |
public List<Restaurant> list; | |
} | |
public class Meal { | |
public int meal_price; | |
public String meal_name; | |
public int meal_id; | |
} | |
public class MenuResponse { | |
public int success; | |
public List<Meal> meal_list; | |
} | |
public class Order { | |
public int number_slip; | |
public int number_slip_index; | |
public int rest_id; | |
public String rest_name; | |
public int status; | |
public Date time; | |
} | |
public class StatusResponse { | |
public boolean success; | |
public List<Order> list; | |
} | |
public class OrderItem { | |
public int amount; | |
public int meal_id; | |
public String meal_name; | |
public int meal_price; | |
} | |
public class StatusDetailResponse { | |
public boolean success; | |
public List<OrderItem> list; | |
} | |
public static class MealAmount { | |
public int meal_id; | |
public int amount; | |
public MealAmount(int meal_id, int amount) { | |
this.meal_id = meal_id; | |
this.amount = amount; | |
} | |
} | |
interface PlateTWOldAPI { | |
@GET("/suggestions.php") | |
void recommendations(Callback<RecommendationResponse> cb); | |
@GET("/restaurants.php") | |
void restaurants(@Query("location") int location, Callback<RestaurantResponse> cb); | |
@GET("/menu.php") | |
void menu(@Query("rest_id") int rest_id, Callback<MenuResponse> cb); | |
@FormUrlEncoded | |
@POST("/status.php") | |
void status(@Field("username") String username, Callback<StatusResponse> cb); | |
@FormUrlEncoded | |
@POST("/status_detail.php") | |
void status_detail(@Field("number_slip_index") int number_slip_index, Callback<StatusDetailResponse> cb); | |
@FormUrlEncoded | |
@POST("/order.php") | |
void order(@Field("username") String username, | |
@Field("rest_id") int rest_id, | |
@Field("order") /* json: [ (meal_id, amount) ...] */ String orders, | |
Callback<Response> cb); | |
@FormUrlEncoded | |
@POST("/cancel.php") | |
void cancel(@Field("number_slip_index") int number_slip_index, | |
Callback<Response> cb); | |
} | |
public static final String API_URI_PREFIX = "http://localhost:8000"; | |
public static final String TEST_USERNAME = "[email protected]"; | |
private static RestAdapter restAdapter; | |
private static PlateTWOldAPI plateTW; | |
private static class DateTimeDeserializer implements JsonDeserializer<Date> { | |
private SimpleDateFormat sdf; | |
public DateTimeDeserializer() { | |
/* http://stackoverflow.com/questions/7910734/gsonbuilder-setdateformat-for-2011-10-26t202959-0700 | |
* http://developer.android.com/reference/java/text/SimpleDateFormat.html | |
* http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html | |
* */ | |
String fmt; | |
if (System.getProperty("java.runtime.name").equals("Android Runtime")) { | |
fmt = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"; | |
} else { | |
fmt = "yyyy-MM-dd'T'HH:mm:ssX"; | |
} | |
sdf = new SimpleDateFormat(fmt, Locale.US); | |
} | |
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx) | |
throws JsonParseException { | |
try { | |
return sdf.parse(json.getAsJsonPrimitive().getAsString()); | |
} catch (ParseException e) { | |
throw new JsonParseException(e.getMessage()); | |
} | |
} | |
} | |
public static PlateTWOldAPI get() { | |
Gson gson = new GsonBuilder() | |
.registerTypeAdapter(Date.class, new DateTimeDeserializer()) | |
.create(); | |
restAdapter = new RestAdapter.Builder() | |
.setServer(API_URI_PREFIX) | |
.setConverter(new GsonConverter(gson)) | |
.build(); | |
plateTW = restAdapter.create(PlateTWOldAPI.class); | |
return plateTW; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tw.plate; | |
import com.google.gson.Gson; | |
import java.util.ArrayList; | |
import java.util.logging.Logger; | |
import retrofit.Callback; | |
import retrofit.RetrofitError; | |
import retrofit.client.Response; | |
import tw.plate.PlateAPI.*; | |
class Test { | |
private static final Logger logger = Logger.getLogger("test"); | |
private static PlateAPI.PlateTWOldAPI plateTW; | |
public static void main(String args[]) { | |
plateTW = PlateAPI.get(); | |
if (false) { | |
testRecommendations(); | |
testRestaurants(); | |
testMenu(); | |
testStatus(); | |
testStatusDetail(); | |
testOrder(); | |
testCancel(); | |
} else { | |
} | |
} | |
static void testRecommendations() { | |
plateTW.recommendations(new Callback<PlateAPI.RecommendationResponse>() { | |
@Override public void success(RecommendationResponse rs, Response response) { | |
logger.info("recommendations: begin"); | |
for (Recommendation r: rs.list) | |
logger.info(String.format("\t(%s, %s, %d, %s)", r.restaurant_name, r.name, r.price, r.pic_uri)); | |
logger.info("recommendations: end"); | |
} | |
@Override public void failure(RetrofitError error) { | |
logger.warning("recommendations: failure"); | |
} | |
}); | |
} | |
static void testRestaurants() { | |
plateTW.restaurants(1, new Callback<RestaurantResponse> () { | |
@Override public void success(RestaurantResponse rs, Response response) { | |
logger.info("restaurants: begin"); | |
for (Restaurant r: rs.list) | |
logger.info(String.format("\t(%d, %d, %s)", r.rest_id, r.location, r.name)); | |
logger.info("restaurants: end"); | |
} | |
@Override public void failure(RetrofitError e) { | |
logger.warning("restaurants: failure"); | |
} | |
}); | |
} | |
private static void testMenu() { | |
plateTW.menu(1, new Callback<MenuResponse>() { | |
@Override public void success(MenuResponse ms, Response response) { | |
logger.info("menu: begin"); | |
for (Meal m: ms.meal_list) | |
logger.info(String.format("\t(%d, %s, %d)", m.meal_id, m.meal_name, m.meal_price)); | |
logger.info("menu: end"); | |
} | |
@Override public void failure(RetrofitError e) { | |
logger.warning("menu: failure"); | |
} | |
}); | |
} | |
private static void testStatus() { | |
plateTW.status(PlateAPI.TEST_USERNAME, new Callback<StatusResponse>() { | |
@Override public void success(StatusResponse ss, Response r) { | |
logger.info("status: begin"); | |
for (Order o: ss.list) | |
logger.info(String.format("\t(%d, %d, %s, %s, %d, %s)", | |
o.number_slip_index, o.status, o.time, o.number_slip, o.rest_id, o.rest_name)); | |
logger.info("status: end"); | |
} | |
@Override public void failure(RetrofitError e) { | |
logger.warning("status: failure"); | |
} | |
}); | |
} | |
private static void testStatusDetail() { | |
plateTW.status_detail(4, new Callback<StatusDetailResponse>() { | |
@Override public void success(StatusDetailResponse ss, Response r) { | |
logger.info("status_detail: begin"); | |
for (OrderItem o: ss.list) | |
logger.info(String.format("(%d, %s, %d, %d)", o.meal_id, o.meal_name, o.meal_price, o.amount)); | |
logger.info("status_detail: end"); | |
} | |
@Override | |
public void failure(RetrofitError error) { | |
logger.warning("status_detail: failure"); | |
} | |
}); | |
} | |
private static void testOrder() { | |
ArrayList<MealAmount> l = new ArrayList<MealAmount>(); | |
l.add(new MealAmount(1, 4)); | |
plateTW.order(PlateAPI.TEST_USERNAME, 1, new Gson().toJson(l), new Callback<Response> () { | |
@Override public void success(Response t, Response r) { | |
logger.info("order: success"); | |
} | |
@Override public void failure(RetrofitError e) { | |
logger.warning("order: failure"); | |
} | |
}); | |
} | |
private static void testCancel() { | |
plateTW.cancel(67, new Callback<Response>() { | |
@Override public void success(Response t, Response r) { | |
logger.info("cancel: success"); | |
} | |
@Override public void failure(RetrofitError e) { | |
logger.warning("cancel: failure"); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment