gpt4 book ai didi

android - 如何通过改造 2 发布请求并获得响应?

转载 作者:行者123 更新时间:2023-11-30 00:07:46 26 4
gpt4 key购买 nike

我的服务只使用 post 方法。

在我的代码中:

我的 ApiInterFace 类

public interface ApiInterface {

@POST("srrvk")
@FormUrlEncoded
Call<JoinAllAllowedInnoResponse> GETJOINALLOWEDINNOLIST(@Field("a") Integer lastOrderId, @Field("b") Integer rowCount,@Header("wkey") String wkey);

}

我的 getArrayList 方法就像

  public void getJoinlist(Integer lastOrderId,Integer rowCount){
showProgressDialog("Please Wait...");
final String wkey = tinyDB.getString(Constant.wkey);
Log.d(CLASS_NAME, "wkey :" +wkey);
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<JoinAllAllowedInnoResponse> call = apiService.GETJOINALLOWEDINNOLIST(lastOrderId,rowCount,wkey);
call.enqueue(new Callback<JoinAllAllowedInnoResponse>() {
@Override
public void onResponse(Call<JoinAllAllowedInnoResponse> call, Response<JoinAllAllowedInnoResponse> response) {
hideProgressDialog();
if (response.body() != null){
if(response.body().getEx() == null){
List<JoinAllAllowedInno> mJoinList = response.body().getJoinList();
mJoinAdapter.setJoinList(mJoinList);
if(mJoinList.size() == 0){
emptyMessage.setVisibility(View.VISIBLE);
}

}else {
getAlert(response.body().getEx());
}
}

}

@Override
public void onFailure(Call<JoinAllAllowedInnoResponse> call, Throwable t) {
hideProgressDialog();
Log.e(CLASS_NAME, t.toString());
}
});
}

我的问题是当我在响应处设置断点时:注意到发生了什么?

问题出在哪里?任何建议或示例代码?

我儿子是:

我的json使用动态头

"ex": null,
"c": [
{
"INNOID": 8,
"SHORTDESC": "***************",
"ORDERID": 1,
"JOINEND": 1519074000000,
"LONGDESC": "******************",
"JOINSTART": 1514754000000,
"DOCLINK": "*****************************",
"TEASERVIDEO": "*****"
},
{
"INNOID": 7,
"SHORTDESC": "***********",
"ORDERID": 2,
"JOINEND": **********,
"LONGDESC": "*****************",
"JOINSTART": 1514790000000,
"DOCLINK": "***********************",
"TEASERVIDEO": "*******************"
}
]
}

最佳答案

首先,输入您的网络服务: 公共(public)接口(interface) ApiInterface {

@POST("webservice/whatever")

另一种选择是给它对象来处理:

Call<JsonElement> GETJOINALLOWEDINNOLIST(@Body YourRequestObject eg);

然后创建2个模型类。一个用于请求,另一个用于响应。使用与 JSON 中相同的名称,或者使用 Jackson 转换器: http://www.jsonschema2pojo.org/

你会在那里弄明白的,因为我不知道你的键名是什么。它会给你 java 类,所以只要下载它们,把它们放在你的模型类文件夹中。然后删除所有不必要的可序列化内容,只保留名称、getter 和 setter。

接下来,创建您的 Retrofit API:

public class RetrofitClassExample{

private static Retrofit retrofit;

final private static String BASE_URL = "https://website.com/";

public static Retrofit getClient(){
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}

}

最后调用的时候,

ClassWhereYourCallIS classWhereYourCallIS= RetrofitClassExample.getClient().create(ClassWhereYourCallIS.class);

final Call<JsonElement> yourRequestObjectResponse = classWhereYourCallIS.GETJOINALLOWEDINNOLIST(yourRequestObject);

yourRequestObjectResponse.GETJOINALLOWEDINNOLIST(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
if (response.isSuccessful()) {
String jsonString = response.body().getAsJsonObject().toString().trim();

JSONObject reader = new JSONObject(jsonString );

YourRequestObjectResponse yourRequestObjectResponse = new YourRequestObjectResponse();
JSONObject rp = reader.getJSONObject("ex");
yourRequestObjectResponse.setSomeValue(rp.getString("keyName"));
}

Not sure if this will help but here you go

关于android - 如何通过改造 2 发布请求并获得响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48685656/

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