retrofit2 不创建对象直接返回字符串

retrofit请求直接返回string

 

有个需求是直接用retrofit去get网页得到html,不创建对象。

用上面文章讲的方法,直接返回ResponseBody

ICollection:

public interface ICollection {

    @GET("/collection/{cid}")
    Call<ResponseBody> getCollection(@Path("cid") String cid);

}

 

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(new OkHttpClient())//不加转换器
                .build();
        ICollection iCollection = retrofit.create(ICollection.class);
        Call<ResponseBody> call = iCollection.getCollection(ID);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {

                try {
                    Message msg = Message.obtain();
                    msg.what = 1;
                    msg.obj = response.body().string();
                    handler.sendMessage(msg);
                }
                catch (IOException e){e.printStackTrace();}
            }

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

            }
        });

 

posted @ 2017-08-03 14:38  qlky  阅读(1076)  评论(0编辑  收藏  举报