1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| package com.app.app_miam.provider;
import android.util.Log;
import com.app.app_miam.entity.Content;
import com.app.app_miam.provider.http.HttpRequest;
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.io.IOException;
import java.io.Reader;
import java.lang.reflect.Type;
import java.sql.Time;
import java.util.Date;
/**
* Created by ml on 18/11/2016.
*/
public class ApiRequest {
final static String BASE_URL = "http://x.x.x.x:8080";
final static String URL_ALL_RESTAURANTS= BASE_URL+"/find-all-restaurant?page=%1$d&size=%2$d";
public Content getRestaurantPageable(long page,long size) {
try {
Content content = null;
Reader result = HttpRequest.get(String.format(URL_ALL_RESTAURANTS,page,size));
// Creates the json object which will manage the information received
GsonBuilder builder = new GsonBuilder();
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer() {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});
builder.registerTypeAdapter(Time.class, new JsonDeserializer() {
@Override
public Time deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
String time = json.getAsString();
String[] parts = time.split(":");
return new Time(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
}
});
Gson gson = builder.create();
content = gson.fromJson(result,Content.class);
HttpRequest.close();
return content;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
} |
Partager