Receiving an empty body in retrofit Response

Viewed 16743

I am using retrofit to get data from http URL. My Interface 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();
}

My request method.

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();
    }
    });
}

In the response I am receiving an empty body.And the server responds with 200 OK.

D/OnResponse: {}

But when I open the URL in browser I am getting JSONObject on the screen.

9 Answers
 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'
Related