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

时间:2018-02-08 12:34:34

标签: android json post retrofit retrofit2

我的服务只是使用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": "*******************"
        }
    ]
}

1 个答案:

答案 0 :(得分:1)

首先,输入您的网络服务:     公共接口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"));
}
  

不确定这是否会有所帮助,但是你去了

相关问题