我是OkHttp库的新手.在使用这个库时,我正在下载网页源代码.这是我的代码:
Request request = new Request.Builder()
.url(urls[0])
.build();
try {
Response response = okHttpClient.newCall(request).execute();
source = response.body().toString();
Log.i("ppp",source);
} catch (IOException e) {
source =e.getMessage();
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但是是在向我展示这个回应:
07-27 11:55:39.133 28050-28069/com.portonics.didar.okhttpdemo I/ppp: okhttp3.internal.http.RealResponseBody@420b639
Run Code Online (Sandbox Code Playgroud)
但后来只需通过.string()更改.toString()即可显示正确的结果.
07-27 11:59:29.451 28050-31483/com.portonics.didar.okhttpdemo I/ppp: <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for." name="description"><meta content="noodp" name="robots"><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script>(function(){window.google={kEI:'JIF5WcLwC8Sd0gT1w6HIAQ',kEXPI:'1301798,1352823,1353383,1353801,1354231,1354277,1354562,1354570,1354619,1354625,1354838,3700277,3700347,3700433,3700442,4029815,4031109,4040138,4043492,4045840,4048347,4063220,4072777,4076999,4078430,4081038,4081164,4093313,4094544,4095910,4096463,4097153,4097922,4097929,4097951,4098721,4098728,4098752,4099391,4102237,4103475,4103845,4103861,4104258,4104658,4105085,4105113,4105241,4106626,4107555,4108124,4108498,4109316,4109490,4110259,4110404,4110656,4111016,4112542,4113148,4113216,4113495,4114597,4114975,4115697,4116365,4116926,4116935,4117280,4117406,4117534,4117912,4117980,4118103,4118227,4118280,4118475,4118626,4118798,4118976,4118977,4119032,4119034,4119036,4119272,4120004,4120046,4120058,4120195,4120278,4120289,4120508,4120578,4120646,4121035,4121036,4121209,4121278,4121316,4121515,4121852,4122007,4122048,4122705,4122724,4123335,4123994,4124100,4124288,10200083,10200096,16200027',authuser:0,kscs:'c9c918f0_24'};google.kHL='en';})();(function(){google.lc=[];google.li=0;google.getEI=function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||google.kEI};google.getLEI=function(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=a.parentNode;return b};google.https=function(){return"https:"==window.location.protocol};google.ml=function(){return null};google.wl=function(a,b){try{google.ml(Error(a),!1,b)}catch(c){}};google.time=function(){return(new …Run Code Online (Sandbox Code Playgroud)