我使用retrofit2创建请求并将参数发送到服务器,如何在onResponse中访问发送参数?
retrofit = new Retrofit.Builder()
.baseUrl("baseAddress")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiBase serviceSetParam = retrofit.create(ApiBase.class);
Call<String> myCall = serviceSetParam.setParam("data1","data2");
Callback<String> myCallback = new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
//i need access data1 & data2 Here !
if (response.isSuccessful()) {
String mResponse= response.body();
} else {
Utils.Log("unSuccessful");
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Utils.Log("onFailure");
}
};
myCall.enqueue(myCallback);
这里是send param方法:
@FormUrlEncoded
@POST("set")
Call<String> setParam(@Field("param1") String param1, @Field("param2") String param2);
答案 0 :(得分:3)
在您的请求的onResponse方法中,测试此代码:
try {
BufferedSink bf = new Buffer();
call.request().body().writeTo(bf);
Log.i("params are",bf.buffer().readUtf8().toString());
} catch (IOException e) {
e.printStackTrace();
}
答案 1 :(得分:2)
您需要从OkHttp获取原始请求。
List<String> pathSegments = original(response.raw()).url().pathSegments();
下式给出:
static Request original(Response response) {
while (true) {
Response prior = response.priorResponse();
if (prior == null) {
break;
}
response = prior;
}
return response.request();
}