http 返回raw java,使用Retrofit获取原始HTTP响应

I want to get the raw http response from my API REST. I have tried with this interface:

@POST("/login")

@FormUrlEncoded

Call login(@Field("username") String login, @Field("password") String pass,

@Field("appName") String appName, @Field("appKey") String appKey);

But I get:

java.lang.IllegalArgumentException: Unable to create call adapter for

retrofit.Call

for method Api.login

I create Retrofit this way:

Retrofit.Builder retrofitBuilder = new Retrofit.Builder();

retrofitBuilder.addConverterFactory(JacksonConverterFactory.create());

Retrofit retrofitAdapter = retrofitBuilder.baseUrl(baseUrl).build();

return retrofitAdapter.create(apiClass);

解决方案

To get access to the raw response, use ResponseBody from okhttp as your call type.

Call login(...)

In your callback, you can check the response code with the code method of the response. This applies to any retrofit 2 return type, because your callback always gets a Response parameterized with your actual return type. For asynchronous --

Call myCall = myApi.login(...)

myCall.enqueue(new Callback() {

@Override

public void onResponse(Response response, Retrofit retrofit) {

// access response code with response.code()

// access string of the response with response.body().string()

}

@Override

public void onFailure(Throwable t) {

t.printStackTrace();

}

});

for synchronous calls --

Response response = myCall.execute();

System.out.println("response code" + response.code());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值