2017-07-27 144 views
4

我是OkHttp庫的新手。 在使用這個庫時,我正在下載一個網頁的源代碼。 這裏是我的代碼:OkHttp的.string()和.toString()有什麼區別?

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: [email protected] 

但後來只是不斷變化的ToString()與.string()是顯示正確的結果。

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 

字符串()的toString()是像我這樣的初學者非常混亂。 有人可以告訴我爲什麼發生這種情況,這兩種方法有什麼區別?

+0

你對這兩種方法有什麼不同嗎? – sForSujit

+0

您可以閱讀這篇文檔https://stackoverflow.com/documentation/java/109/strings/3124/adding-tostring-method-for-custom-objects – Rocherlee

+1

toString()方法寫在Object類中,它是父類當我們不覆蓋這個方法時,java中的每個類都會在內部爲對象創建一個哈希碼並將其作爲字符串返回。但是當你調用由RealResponseBody類定義的string()方法時,你會得到實際的響應。我希望它消除你的困惑 –

回答

6
string()

ResponseBody方法,該方法,引用的文檔:

返回響應作爲與Content-Type頭的字符集解碼的字符串。如果該頭文件缺失或缺少字符集,則會嘗試根據其BOM或UTF-8對響應主體進行解碼。

toString()java.lang.Object繼承,而不是由ResponseBody重寫,所以它會返回默認implementetation(類似[email protected]{some hash code}

4

.toString():這將返回字符串格式的對象。

.string():這將返回您的服務器響應。

1

的ToString():這將返回你的對象以字符串格式

.string():返回ResponseBody,它是服務器的響應。

1
.toString() Explanation : 

如果你wan t將任何對象表示爲一個字符串,toString()方法就存在了。

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

如果您打印任何對象,java編譯器會在內部調用對象上的toString()方法。因此,重寫toString()方法,返回所需的輸出,它可以是對象的狀態等,取決於您的實現。 Java的toString()方法

通過覆蓋Object類的toString()方法的

優勢,我們可以返回對象的值,所以我們並不需要編寫大量代碼。

string() Explanation : 

在java中,字符串基本上是一個表示char值序列的對象。字符數組的作用與java字符串相同。例如:

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