他们,你能帮我改造吗?我正在尝试实现Post请求,但我不知道如何正确执行。
@FormUrlEncoded
@POST("/tools/")
SaleEvent updateUser(@Field("field1") String field1, @Field("field2") String field2);
类SaleEvent与JsonArray中的json对象具有相同的字段“更新” 问题是 - 我有一个复杂的响应,不容易解析gson:
{
"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"
},
]
}
我需要的是在ArrayList中获得响应
@FormUrlEncoded
@POST("/tools/")
ArrayList<SaleEvent> updateUser(@Field("field1") String field1, @Field("field2") String field2);
像这样的Smth
问题解决了: 问题在于使用ArrayList而不是List
答案 0 :(得分:1)
您可以使用TypeAdapterFactory从复杂的JSON中提取数据。
这是班级的简单实施
ItemTypeAdapterFactory.java
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").isJsonObject())
{
jsonElement = jsonObject.get("updates");
}
}
return delegate.fromJsonTree(jsonElement);
}
}.nullSafe();
}
}
然后你需要创建一个GSON Builder.Here是它的一个例子。
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new ItemTypeAdapterFactory()) // This is the important line ;)
.create();
最后将GSON Builder附加到RequestAdapter,如下:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(BASE_URL)
.setConverter(new GsonConverter(gson))
.build();
现在您必须声明GSON响应对象和请求参数:
@FormUrlEncoded
@POST("/tools/")
private Void updateUser(@Field("field1") String field1, @Field("field2") String field2,Callback<JSONResponse> fbLogin);
这是 JSONResponse.java
class JSONResponse {
@SerializedName("update")
ArrayList<SaleEvent> array;
}
然后声明 SaleEvent.java
class SaleEvent {
@SerializedName("uid")
int uid;
@SerializedName("shop_name")
String uid;
@SerializedName("version")
int version;
....
}
然后提出要求......希望一切顺利
这将适用于你