Retrofit2 returns 999 当我调用 api

Retrofit2 returns 999 when I called api

我使用 Retrofit2 调用 api,当我做 apiTest("http://xxx.xx.xx.xxx:xxxx/", "T001", "Futek10911-01" ),response.code 是 999,但它 returns 在 Postman 中是正确的值。问题出在哪里?

    private void apiTest(String url, String machId, String check) throws JSONException {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    MyAPIService myAPIService = retrofit.create(MyAPIService.class);
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("MACHID", machId);
    jsonObject.put("CHECK", check);

    Call<GetHostTime> call = myAPIService.getHostTime("sRequest", jsonObject);
    call.enqueue(new Callback<GetHostTime>() {
        @Override
        public void onResponse(Call<GetHostTime> call, Response<GetHostTime> response) {
            if(response.isSuccessful()){
                Log.d("response ", "isSuccessful");
            }else {
                Log.d("response code ", response.code() + "");
            }
        }

        @Override
        public void onFailure(Call<GetHostTime> call, Throwable t) {
            Log.d("Failure", t.getMessage());
        }
    });

public interface MyAPIService {
@POST("TcLeaseParkAPI/api/ParkingAPI/GetHostTime")
@FormUrlEncoded
Call<GetHostTime> getHostTime(@Field("MACHID") String key, @Field("CHECK") JSONObject jsonObject);

}

正在比较您的 Postman 请求和您的代码。很明显我觉得你发送请求的方式不对。

所以我们修改您的Retrofit请求如下

public interface MyAPIService {
@POST("TcLeaseParkAPI/api/ParkingAPI/GetHostTime")
@FormUrlEncoded
Call<GetHostTime> getHostTime(@Field("sRequest") JSONObject jsonObject);

然后你的请求调用如下

 Call<GetHostTime> call = myAPIService.getHostTime(jsonObject);

现在您的 JSONObject 将进入密钥 sRequest