在post retrofit2上获得了null响应体

时间:2016-04-23 10:33:38

标签: android response retrofit2

我想在服务器上发帖子,并让响应主体显示它 但仍然总是得到空字符串

这是我的界面类

    @POST("peserta/cek")
    Call<CekPeserta> cekPeserta(@Body String kode_tiket);


   class Factory {

        private static PesertaAPI service;
        public static PesertaAPI getInstance(){

            if (service == null) {

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

                service = retrofit.create(PesertaAPI.class);
                return service;
            }
            else return service;
        }
    }

和mainClass

PesertaAPI.Factory.getInstance().cekPeserta("SDL8S3").enqueue(new Callback<CekPeserta>() {
            @Override
            public void onResponse(Call<CekPeserta> call, Response<CekPeserta> response) {
                Log.i("post status","sukses");
                Log.i("nama = ", String.valueOf(response.body().getName()));



            }

            @Override
            public void onFailure(Call<CekPeserta> call, Throwable t) {

            }
        });

1 个答案:

答案 0 :(得分:2)

它依赖于ur服务器接受哪些数据,如果它的FormUrl编码然后调用...

@POST("peserta/cek")
@FormUrlEncoded
Call<CekPeserta> cekPeserta(@Body // your data );

如果它在JSON Form中接受,则调用如..

@POST("peserta/cek")
@Headers("Content-Type: application/json")
Call<CekPeserta> cekPeserta(@Body // your data);

了解更多信息..和详细信息click-here