问题描述
我对来自 dhl 的 api 有问题,我从 dhl 创建了 GET api,当在控制台中打印时,结果将打印,但是当使用浏览器时,我得到了这样的响应:
com.squareup.okhttp.internal.http.RealResponseBody@68bd3d26
这是我的代码:
@RequestMapping("/getData")
public String getAcc() throws IOException
{
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
HttpUrl httpUrl = new HttpUrl.Builder()
.scheme("https")
.host("api-eu.dhl.com")
.addpathSegment("track")
.addpathSegment("shipments")
.addQueryParameter("trackingNumber","cencored")
.addQueryParameter("service","express")
.build();
Request request = new Request.Builder()
.addHeader("content-type","application/json")
.addHeader("Connection","close")
.addHeader("dhl-API-Key","cencored")
.addHeader("ConsumerKey","cencored")
.addHeader("ConsumerSecret","cencored")
.removeHeader("content-encoding")
.removeHeader("Content-Length")
.url(httpUrl) // <- Finally put httpUrl in here
.build();
response = client.newCall(request).execute();
System.out.println(response.body().string());
return this.response.body().toString();
}
解决方法
解决了... 这很奇怪,但对我有用。 所以我们不能调用“response.body().string();”两次。