简体   繁体   English

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

[英]How to post request and get response with retrofit 2?

My service just use post method.我的服务只是使用 post 方法。

in my code :在我的代码中:

my ApiInterFace Class我的 ApiInterFace 类

public interface ApiInterface {

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

}

and my getArrayList method is like我的 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());
           }
       });
    }

my question is when I put a breakpoint at response : noting happened ?我的问题是当我在响应中设置断点时:注意到发生了吗?

Where is the problem ?问题出在哪儿 ? Any advice or sample code ?任何建议或示例代码?

and my son is :我的儿子是:

My json uses Dynamic header我的 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": "*******************"
        }
    ]
}

First of all, enter your web service: public interface ApiInterface {首先,输入你的web服务:public interface ApiInterface {

@POST("webservice/whatever")

Another alternative is to give it objects to work with:另一种选择是为其提供要使用的对象:

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

Then create 2 model classes.然后创建 2 个模型类。 One for the request and the other for response.一个用于请求,另一个用于响应。 Use the same names as that in the JSON or alternative is to use a Jackson converter: http://www.jsonschema2pojo.org/使用与 JSON 相同的名称,或者使用 Jackson 转换器: http : //www.jsonschema2pojo.org/

You'll figure it out there because I don't know what your key names are.你会在那里弄清楚,因为我不知道你的关键名称是什么。 It will give you the java classes so just download them, put them in your model class folder.它会给你java类,所以只需下载它们,把它们放在你的模型类文件夹中。 Then remove all the unnecessary serializable stuff and keep only the names and getters and setters.然后删除所有不必要的可序列化的东西,只保留名称和 getter 和 setter。

Next, create your Retrofit API:接下来,创建您的 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;
}

}

Then finally when calling,然后最后在打电话时,

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不知道这是否会有所帮助,但你去吧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM