2
我試圖實現從服務器使用Retrofit獲取arayList。 這裏是我做的:通過使用改進獲取arraylist
適配器類
public class ItemTypeAdapterFactory implements TypeAdapterFactory {
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
public T read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("updates") && jsonObject.get("updates").isJsonArray())
{
jsonElement = jsonObject.getAsJsonArray("updates");
}
}
return delegate.fromJsonTree(jsonElement);
}
}.nullSafe();
}
}
接口(API)
public interface Api {
@FormUrlEncoded
@POST("/tools/gps_event_api/")
public void updateUser(@Field("tag") String tag, @Field("version") String version, Callback<JSONResponse> fbLogin);
}
JsonResponse
public class JSONResponse {
@SerializedName("update")
SaleEven array;
}
用法:
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new ItemTypeAdapterFactory()) // This is the important line ;)
.create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://www.poputchik-online.com")
.setConverter(new GsonConverter(gson))
.build();
Api service = restAdapter.create(Api.class);
retrofit.Callback<JSONResponse> callback=new retrofit.Callback<JSONResponse>() {
@Override
public void success(JSONResponse jsonResponse, Response response) {
Log.e("SUCESS",jsonResponse.toString());
}
@Override
public void failure(RetrofitError error) {
error.printStackTrace();
}
};
service.updateUser(UPDATE_TAG, "-1",callback);
我的JSON響應結構:
{
"tag": "check_update",
"success": 1,
"error": 0,
"updates": [
{
"uid": "47",
"shop_name": "Ashan",
"shop_address": "\u041c\u043e\u0441\u043a\u043e\u0432\u0441\u043a\u0438\u0439 \u043f\u0440\u043e\u0441\u043f\u0435\u043a\u0442 25",
"name": "\u0410\u0448\u0430\u043d",
"vip_priority": "1",
"event_type": "0",
"lat": "52.4978812",
"lon": "13.4055422",
"image_url": "uploads\/images3.jpeg",
"city_id": "2",
"version": "0",
"date_from": "2014-12-28 00:00:00",
"date_to": "2014-12-29 00:00:00",
"created_at": "2014-07-10 00:00:00",
"updated_at": "2014-07-10 00:00:00",
"comment_long": "ewrterwt",
"comment_short": "wertewrt"
},
{
"uid": "48",
"shop_name": "Kvadrat",
"shop_address": "\u0432\u0443\u043b. \u0414\u0438\u043c\u0438\u0442\u0440\u043e\u0432\u0430, 7",
"name": "\u041a\u0432\u0430\u0434\u0440\u0430\u0442",
"vip_priority": "1",
"event_type": "0",
"lat": "52.7678812",
"lon": "13.3855422",
"image_url": "/uploads\/images1.jpeg",
"city_id": "2",
"version": "0",
"date_from": "2014-12-28 00:00:00",
"date_to": "2014-12-29 00:00:00",
"created_at": "2014-07-10 00:00:00",
"updated_at": "2014-07-10 00:00:00",
"comment_long": "ewrtwret",
"comment_short": "Privet"
},
]
}
SaleEven類:
@SerializedName("uid")
private long uid;
@SerializedName("name")
private String name;
@SerializedName("comment_short")
private String comment;
@SerializedName("comment_long")
private String longComment;
@SerializedName("shop_name")
private String shopName;
@SerializedName("shop_address")
private String shopAddress;
....
我得到的錯誤:預期的目標,但發現陣列(GSON)
感謝它的幫助。而且我還需要避免使用GSON的自定義適配器,即使沒有自定義適配器,它也能工作。 –
嗨,我有一個類似的JSON結構作爲響應,我正在努力形成一個POJO,我也在使用GSON。你也提到你必須使用自定義適配器,你是怎麼做到的?謝謝 – Deep
@Deep只是使用json的一些代碼生成器,例如, http://www.jsonschema2pojo.org/ –