1. 程式人生 > >Retrofit post 新增請求頭請求體樣例

Retrofit post 新增請求頭請求體樣例

Retrofit retrofit1 = new Retrofit.Builder()
        .baseUrl("http://39.107.224.233/")
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .build();
BodyService bodyService = retrofit.create(BodyService.class);
Gson gson = new Gson();
HashMap<String, String> map = new HashMap<>();
map.put("userId","aadc2d9fe76f4b89bf37ca738e23eafe");
map.put("channelId","b9240eee3b0211e8b64c00163e30445d");
map.put("cursor","0");
String strEntity = gson.toJson(map);

body = RequestBody.create(okhttp3.MediaType.parse("Content-Type:application/json"),strEntity);
bodyService.call(body).enqueue(new Callback<BodyBean>() {
    @Override
    public void onResponse(Call<BodyBean> call, Response<BodyBean> response) {
        List<BodyBean.DataBean.NewListBean> newList = response.body().getData().getNewList();
        Log.e("MainActivity", newList.size()+"");
    }

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

    }
});

(Service):

@Headers("Content-Type:application/json")
@POST("firstga/app/news/downListNews")
Call<BodyBean> call(@Body()RequestBody body);