OkHttp的.string()和.toString()有什么区别?

时间:2017-07-27 06:04:36

标签: java android tostring okhttp3

我是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();
}

但是向我展示了这个回应:

07-27 11:55:39.133 28050-28069/com.portonics.didar.okhttpdemo I/ppp: okhttp3.internal.http.RealResponseBody@420b639

但稍后只需通过.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 Date).getTime()};google.log=function(a,b,c,d,g){if(a=google.logUrl(a,b,c,d,g)){b=new Image;var e=google.lc,f=google.li;e[f]=b;b.onerror=b.onload=b.onabort=function(){delete e[f]};google.vel&&google.vel.lu&&google.vel.lu(a);b.src=a;google.li=f+1}};google.logUrl=function(a,b,c,d,g){var e="",f=google.ls||"";c||-1!=b.search("&ei=")||(e="&ei="+google.getEI(d),-1==b.search("&lei=")&&(d=google.getLEI(d))&&(e+="&lei="+d));a=c||"/"+(g||"gen_204")+"?atyp=i&ct="+a+"&cad="+b+e+f+"&zx="+google.time();/^http:/i.test(a)&&google.https()&&(google.ml(Error("a"),!1,{src:a,glmm:1}),a="");return a};google.y={};google.x=function(a,b){if(a)var c=a.id;else{do c=Math.random();while(google.y[c])}google.y[c]=[a,b];return!1};google.lq=[];google.load=function(a,b,c){google.lq.push([[a],b,c])};google.loadAll=function(a,b){google.lq.push([a,b])};}).call(this);google.f={};var a=window.location,b=a.href.indexOf("#");if(0<=b){var c=a.href.substring(b+1);/(^|&)q=/.test(c)&&-1==c.indexOf("#")&&a.replace("/search?"+c.replace(/(^|&)fp=[^&]*/g,"")+"&cad=h")};</script><style>#gbar,#guser{font-size:13px;padding-top:1px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22px;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important}
                                                                     </style><style>body,td,a,p,.h{font-family:arial,sans-serif}body{margin:0;overflow-y:scroll}#gog{padding:3px 8px 0}td{line-height:.8em}.gac_m td{line-height:17px}form{margin-bottom:20px}.h{color:#36c}.q{color:#00c}.ts td{padding:0}.ts{border-collapse:collapse}em{font-weight:bold;font-style:normal}.lst{height:25px;width:496px}.gsfi,.lst{font:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.ds{display:inline-box;display:inline-block;margin:3px 0 4px;margin-left:4px}input{font-family:inherit}a.gb1,a.gb2,a.gb3,a.gb4{color:#11c !important
对于像我这样的初学者来说,

string() toString()非常令人困惑。 有人可以告诉我为什么会发生这种情况,这两种方法有什么区别?

4 个答案:

答案 0 :(得分:6)

string()ResponseBody的一种方法,引用文档:

  

将响应作为使用Content-Type标头的字符集解码的字符串返回。如果该标头不存在或缺少字符集,则会尝试根据其BOM或UTF-8对响应主体进行解码。

toString()继承自java.lang.Object,未被ResponseBody覆盖,因此会返回默认实施(类似okhttp3.ResponseBody@{some hash code}

答案 1 :(得分:4)

.toString():这将以字符串格式返回您的对象。

.string():这会返回您的服务器响应。

答案 2 :(得分:1)

.toString():以字符串格式返回您的对象。

.string():返回ResponseBody,它是来自服务器的响应。

答案 3 :(得分:1)

.toString() Explanation :

如果要将任何对象表示为字符串,则toString()方法就会出现。

toString()方法返回对象的字符串表示形式。

如果打印任何对象,java编译器会在内部调用对象上的toString()方法。所以重写toString()方法,返回所需的输出,它可以是对象的状态等取决于你的实现。

Java toString()方法的优点

通过覆盖Object类的toString()方法,我们可以返回对象的值,因此我们不需要编写很多代码。

string() Explanation :

在java中,string基本上是一个表示char值序列的对象。字符数组与java字符串的作用相同。例如:

char[] ch={'j','a','v','a','t','p','o','i','n','t'};  
String s=new String(ch);