gpt4 book ai didi

android - 使用改造自定义解析器发布请求

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:08:08 27 4
gpt4 key购买 nike

伙计们,你能帮我改造一下吗?我正在尝试执行 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);

像这样

问题解决:问题在于使用 ArrayList 而不是 List

最佳答案

您可以使用 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。这是它的示例。

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;
....
}

然后strt请求...希望一切顺利

这对你有用

关于android - 使用改造自定义解析器发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27648291/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com