我正在使用OkHttp来获取一些网站的内容.
@H_403_6@OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://www.google.at")
.build();
Response httpResponse = client.newCall(request).execute();
String html = httpResponse.body().string();
这种方法:
@H_403_6@httpResponse.toString();返回以下内容:
@H_403_6@Response{protocol=http/1.1,code=200,message=OK,url=https://www.google.at}有没有办法将statusCode作为整数,还是需要一个正则表达式来过滤掉这个toString() – 方法?
解决方法
您可以使用
HttpResponse类,并使用它可以访问状态代码如下;
@H_403_6@HttpResponse httpResponse = client.newCall(request).execute();
httpResponse.getStatusLine().getStatusCode();
如果您正在使用com.squareup.okhttp.Response,那么可以使用code()方法获取HTTP状态代码.
@H_403_6@Response httpResponse = client.newCall(request).execute(); httpResponse.code();