在改装响应中接收空体

Receiving an empty body in retrofit Response

我正在使用改装从 http URL 获取数据。 我的界面 Class :

public interface SlotsAPI {

    /*Retrofit get annotation with our URL
      And our method that will return a Json Object
    */
    @GET(url)
    retrofit.Call<JSONObject> getSlots();
}

我的请求方式。

public void getResponse(){

    Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

    //Creating an object of our api interface
    SlotsAPI api = retrofit.create(SlotsAPI.class);
    retrofit.Call<JSONObject> callback = api.getSlots();
    callback.enqueue(new Callback<JSONObject>() {
    @Override
    public void onResponse(Response<JSONObject> response) {
        if (response != null) {
            Log.d("OnResponse", response.body().toString());
        }
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
    });
}

在我收到空的响应中 body.And 服务器响应 200 OK。

D/OnResponse: {}

但是当我在浏览器中打开 URL 时,我在屏幕上看到了 JSONObject。

我认为您不了解改装方案。 正确的接口应该是:

public interface SlotsAPI {

    /*Retrofit get annotation with our URL
      And our method that will return a Json Object
    */
    @GET(url)
    JSONObject getSlots();
}

当您调用 getSlots 方法时,retrofit 将自动执行 HTTP 请求和 return JSONObject。 您将需要在主线程之外执行此操作。

确保@Get的url是相对路径

  • @Base URL:总是以/

  • 结尾
  • @Url:不要以 /

  • 开头

示例:

  String URL = http://api.co/base/ ;

  @GET("webservice/syncdown")
  JSONObject getSlots();

您可能会收到插槽列表。如果您发送 json

数组,Gson 转换器将处理它
 @GET(url)
 retrofit.Call<List<Slot>> getSlots();

你应该这样试....

public interface SlotsAPI {

/*Retrofit get annotation with our URL
  And our method that will return a Json Object
*/
@GET(url)
Call<JsonElement> getSlots();
}

在请求方法中

 retrofit.Call<JsonElement> callback = api.getSlots();
callback.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Response<JsonElement> response) {
    if (response != null) {
        Log.d("OnResponse", response.body().toString());
    }
}

您使用的是改造 2 还是改造 1?版本 2 仍处于测试阶段。 如果您使用的是版本 1. 使用此:

public interface SlotsAPI {

    /*Retrofit get annotation with our URL
      And our method that will return a Json Object
    */
    @GET(url)
    void getSlots(Callback<JsonElement> callback);
}

有了这个调用将是异步的。

同样的问题, 救了我一命。
有关同一主题的更多信息:如果您需要从一对中获取值,请使用:

String value = response.body().getAsJsonObject().get("pair_name").getAsString();

请检查您的 JsonObject。如果你想在 json 中获得响应,你必须定义一个响应类型 JsonObject 而不是 JSONObject 否则在你的界面中指定 pojo class。

Call getSlots() 对我有用。

 private void APIRetrofit_method() {
      Retrofit  retrofit = new Retrofit.Builder()
                .baseUrl(RecyclerInterface.JSONURL)
               // .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RecyclerInterface api = retrofit.create(RecyclerInterface.class);

        Call<ResponseBody> call = api.getString(); /// GET METHOD without passing params
        // Post METHOD CODE START
//        HashMap<String, String> params = new HashMap<String, String>();
//        params.put("name", "yuva");
//        params.put("pass", "" + "123");
     //   Call<ResponseBody> call1 = api.getProspectList(params);
        // Post METHOD CODE END
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                   try {
                        Log.d(TAG, "GetProspectlistresponse" + "" + response.isSuccessful());
                        utility.hideProgressDialog();
                        if (response.isSuccessful()) {

                            String remoteResponse = new String(response.body().string());
                            Log.d(TAG, "Holidaylistresponse" + "" + remoteResponse);
                            try {

            JSONObject object = new JSONObject(remoteResponse);
            JSONArray array = object.getJSONArray("Holidays_Details");

            if (array.toString().equals("[]")) {
                holiday_recyclerView.setVisibility(View.GONE);
            } else {
                holiday_recyclerView.setVisibility(View.VISIBLE);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject c = array.getJSONObject(i);
                    String holidayDate = c.getString(TAG_HOLIDAYDATE);
                    String holidayName = c.getString(TAG_HOLIDAYName);
                    String holidaytype = c.getString(TAG_HOLIDAYtype);

                    HashMap<String, String> customers = new HashMap<String, String>();

                    customers.put(TAG_HOLIDAYDATE, holidayDate);
                    customers.put(TAG_HOLIDAYName, holidayName);
                    customers.put(TAG_HOLIDAYtype, holidaytype);
                    arrayList.add(customers);
                }

                getHolidaylistAdapter.notifyDataSetChanged();
            }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            utility.hideProgressDialog();
    }
                } catch (IOException e) {
                            e.printStackTrace();
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i("ErrorResponsestring", call.toString());
            }
        });
    }
String JSONURL = "https://demonuts.com/Demonuts/JsonTest/Tennis/";
    @GET("json_parsing.php")
    Call<ResponseBody> getString();
//    @POST("getProspectList")
//    @FormUrlEncoded
//    Call<ResponseBody> getProspectList(@FieldMap HashMap<String, String> body);
 implementation 'com.squareup.retrofit2:retrofit:2.0.2'
    implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
    implementation 'com.squareup.okhttp3:okhttp:4.0.0'