Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

web page to a String

Hi,

I was looking at below URL
http://www.avajava.com/tutorials/lessons/how-do-i-convert-a-web-page-to-a-string.html?page=1


char[] charArray = new char[1024];
                  StringBuffer sb = new StringBuffer();
                  while ((numCharsRead = isr.read(charArray)) > 0) {
                        sb.append(charArray, 0, numCharsRead);



I am not clear what they are doing in above steps.

please advise
Any links resources ideas highly appreciated. Thanks in advance
Avatar of rrz
rrz
Flag of United States of America image

From the link you posted, as I see it.  
URLConnection object is used to obtain the contents of the web page.  
InputStreamReader reads the content and puts it into charArray.
charArray is added to sb  
sb is converted to String
result is printed  

I guess the confusing lines are
 
>while ((numCharsRead = isr.read(charArray)) > 0) {  
this line uses  InputStreamReader's read() method. From the API  we see that we are using the read method that is  inherited from java.io.Reader .   This method reads characters into an array.  It returns the number of characters that it has read, or -1 if the end of the stream has been reached.  In this case numCharsRead is the number returned. So the while statement keeps executing the read until the whole array is read.

>sb.append(charArray, 0, numCharsRead);  
This uses StringBuffer's method append.   charArray is the array to be appended. 0 is the index of the first char to append.  numCharsRead is the number of chars to append.
Actually, from looking at  the API,  we could have used the other append method and simply have used
sb.append(charArray);
Avatar of gudii9

ASKER

i checked the api
http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
hwich append they are using
public StringBuffer append(char[] str,
                  int offset,
                  int len)

Appends the string representation of a subarray of the char array argument to this sequence.

Characters of the char array str, starting at index offset, are appended, in order, to the contents of this sequence. The length of this sequence increases by the value of len.

The overall effect is exactly as if the arguments were converted to a string by the method String.valueOf(char[],int,int), and the characters of that string were then appended to this character sequence.

Parameters:
    str - the characters to be appended.
    offset - the index of the first char to append.
    len - the number of chars to append.
Returns:
    a reference to this object.
Throws:
    IndexOutOfBoundsException - if offset < 0 or len < 0 or offset+len > str.length

is this is above one. please advise
Avatar of gudii9

ASKER

we could have used the other append method and simply have used
sb.append(charArray);

How to understand and get the gist of API and decide which one is the best for particular situation. I am not very clear on that. please advise
>is this is above one. please advise  
Yes.  
At the API link that you posted, there are two append methods that accept a char []  as a parameter. In my opinion, the other one should have used because we are appending the whole array.  Furthermore, we should use StringBuilder  instead of StringBuffer.
http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html   
Maybe the code that you linked  is old code.
Avatar of gudii9

ASKER

when i run this example i see error in my console as below
java.net.ConnectException: Connection timed out: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)

Furthermore, we should use StringBuilder  instead of StringBuffer.

why do you think so. Please advise

I have not understood gist of this example what author is trying to do. Get google url (www.google.com) and convert to string. why we need character array here. Please advise
>java.net.ConnectException: Connection timed out: connect  
What URL did you try to connect to ?  Did you try just typing the URL into the address bar of your browser?  Could you access the page that way?
I copyed, pasted, compiled and ran the exact code from the page you linked. It worked for me.  

>why do you think so. Please advise
I read the API.http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html    
A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.
>I have not understood gist of this example what author is trying to do  
He is demonstrating a way of reading content from a URL.  
>why we need character array here  
We could read and append one character at a time, but it more it faster and more efficient to use array.
Avatar of gudii9

ASKER

>I have not understood gist of this example what author is trying to do  
He is demonstrating a way of reading content from a URL.

content of the URL((say www.aaa.com) or content of the URL page (when url page opens it opens with membership, discounts, shopping, automative, travel etc) that is displayed using the URL. please advise
>content of the URL((say www.aaa.com) or content of the URL page  
A server sends a response back to a client that made the request. That response is the content. It is the source code for a browser.
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, you right. I didn't think about it long enough and made a bad suggestion. I am sorry.
Avatar of gudii9

ASKER

Rank: Genius
rrz@871311 Member Since: 05/02/2000 1317 Solution(s) >java.net.ConnectException: Connection timed out: connect  
What URL did you try to connect to ?  Did you try just typing the URL into the address bar of your browser?  Could you access the page that way?

I just tried the example as it is in my eclipse IDE



public class ConvertUrlToString {

	public static void main(String[] args) {

		try {
			String webPage = "http://www.google.com";
			URL url = new URL(webPage);
			URLConnection urlConnection = url.openConnection();

Open in new window



i was able to access google.com out side ide from my browser without any issue. I tried on my office laptop. do you think that might block connection. I am not sure. please advise



eg. say your stream had the following characters in it...  ABCDEFGHI  and you had a charArray of size 4...

On the first isr.read...
charArray = [A,B,C,D]
numCharsRead = 4
so using either sb.append call the string currently holds...
"ABCD"

On the next isr.read...
charArray = [E,F,G,H]
numCharsRead = 4
so using either sb.append call the string currently holds...
"ABCDEFGH"

On the next (and last) isr.read...
charArray = [I,F,G,H]             (notice that "I" was read in but F G H are still there from before)
numCharsRead = 1
so if we just did sb.append(charArray) we would end up with...
"ABCDEFGHIFGH"
rather than if we use sb.append(charArray, 0 numCharsRead) where numCharsRead = 1 we get...
"ABCDEFGHI"  (correctly!)
I do not understand this example clearly. Can you please elaborate.



>content of the URL((say www.aaa.com) or content of the URL page  
A server sends a response back to a client that made the request. That response is the content. It is the source code for a browser



does it changes the image content also to the string. please advise
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

I changed to
      String webPage = "http://acme.com/mailto/?id=wa";

Still i am getting similar error


java.net.ConnectException: Connection timed out: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)


I will try from home laptop later and let you know.
Avatar of gudii9

ASKER

I tried with www.google.com as given in link from home laptop. I got output
*** BEGIN ***
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage"><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 itemprop="image" content="/images/google_favicon_128.png"><title>Google</title><script>(function(){
window.google={kEI:"iPYHU9qYJrPJ0gHuroCwCQ",getEI:function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||google.kEI},https:function(){return"https:"==window.location.protocol},kEXPI:"17259,4000116,4007661,4007830,4008067,4008133,4008142,4009033,4009565,4009641,4010806,4010830,4010858,4010899,4011228,4011258,4011679,4011863,4012001,4012316,4012318,4012504,4012538,4012548,4013338,4013374,4013414,4013416,4013591,4013723,4013747,4013758,4013787,4013823,4013842,4013868,4013917,4013967,4013979,4014016,4014092,4014273,4014307,4014368,4014431,4014513,4014515,4014518,4014597,4014636,4014649,4014671,4014792,4014794,4014804,4014813,4014823,4014825,4014848,4014989,4015119,4015121,4015135,4015228,4015234,4015260,4015299,4015300,4015324,4015326,4015354,4015444,4015589,7000197,8300007,8500073,8500157,10200002,10200012,10200029,10200030,10200040,10200053,10200054,10200066,10200083",kCSI:{e:"17259,4000116,4007661,4007830,4008067,4008133,4008142,4009033,4009565,4009641,4010806,4010830,4010858,4010899,4011228,4011258,4011679,4011863,4012001,4012316,4012318,4012504,4012538,4012548,4013338,4013374,4013414,4013416,4013591,4013723,4013747,4013758,4013787,4013823,4013842,4013868,4013917,4013967,4013979,4014016,4014092,4014273,4014307,4014368,4014431,4014513,4014515,4014518,4014597,4014636,4014649,4014671,4014792,4014794,4014804,4014813,4014823,4014825,4014848,4014989,4015119,4015121,4015135,4015228,4015234,4015260,4015299,4015300,4015324,4015326,4015354,4015444,4015589,7000197,8300007,8500073,8500157,10200002,10200012,10200029,10200030,10200040,10200053,10200054,10200066,10200083",ei:"iPYHU9qYJrPJ0gHuroCwCQ"},authuser:0,ml:function(){},kHL:"en",time:function(){return(new Date).getTime()},log:function(a,b,c,h,k){var d=
new Image,f=google.lc,e=google.li,g="";d.onerror=d.onload=d.onabort=function(){delete f[e]};f[e]=d;c||-1!=b.search("&ei=")||(g="&ei="+google.getEI(h));c=c||"/"+(k||"gen_204")+"?atyp=i&ct="+a+"&cad="+b+g+"&zx="+google.time();a=/^http:/i;a.test(c)&&google.https()?(google.ml(Error("GLMM"),!1,{src:c}),delete f[e]):(d.src=c,google.li=e+1)},lc:[],li:0,y:{},x:function(a,b){google.y[a.id]=[a,b];return!1},load:function(a,b,c){google.x({id:a+l++},function(){google.load(a,b,c)})}};var l=0;})();
(function(){google.sn="webhp";google.timers={};google.startTick=function(a,b){google.timers[a]={t:{start:google.time()},bfr:!!b}};google.tick=function(a,b,g){google.timers[a]||google.startTick(a);google.timers[a].t[b]=g||google.time()};google.startTick("load",!0);
try{}catch(d){}})();
var _gjwl=location;function _gjuc(){var a=_gjwl.href.indexOf("#");if(0<=a&&(a=_gjwl.href.substring(a),0<a.indexOf("&q=")||0<=a.indexOf("#q="))&&(a=a.substring(1),-1==a.indexOf("#"))){for(var d=0;d<a.length;){var b=d;"&"==a.charAt(b)&&++b;var c=a.indexOf("&",b);-1==c&&(c=a.length);b=a.substring(b,c);if(0==b.indexOf("fp="))a=a.substring(0,d)+a.substring(c,a.length),c=d;else if("cad=h"==b)return 0;d=c}_gjwl.href="/search?"+a+"&cad=h";return 1}return 0}
function _gjh(){!_gjuc()&&window.google&&google.x&&google.x({id:"GJH"},function(){google.nav&&google.nav.gjh&&google.nav.gjh()})};
window._gjh&&_gjh();</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}body{background:#fff;color:black}a{color:#11c;text-decoration:none}a:hover,a:active{text-decoration:underline}.fl a{color:#36c}a:visited{color:#551a8b}a.gb1,a.gb4{text-decoration:underline}a.gb3:hover{text-decoration:none}#ghead a.gb2:hover{color:#fff !important}.sblc{padding-top:5px}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11px}.lsbb{background:#eee;border:solid 1px;border-color:#ccc #999 #999 #ccc;height:30px}.lsbb{display:block}.ftl,#fll a{display:inline-block;margin:0 12px}.lsb{background:url(/images/srpr/nav_logo80.png) 0 -258px repeat-x;border:none;color:#000;cursor:pointer;height:30px;margin:0;outline:0;font:15px arial,sans-serif;vertical-align:top}.lsb:active{background:#ccc}.lst:focus{outline:none}#addlang a{padding:0 3px}</style><script></script> </head><body bgcolor="#fff"><script>(function(){var src='/images/srpr/nav_logo80.png';var iesg=false;document.body.onload = function(){window.n && window.n();if (document.images){new Image().src=src;}
if (!iesg){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();}
}
})();</script><textarea id="csi" style="display:none"></textarea><div id="mngb"> <div id=gbar><nobr><b class=gb1>Search</b> <a class=gb1 href="http://www.google.com/imghp?hl=en&tab=wi">Images</a> <a class=gb1 href="http://maps.google.com/maps?hl=en&tab=wl">Maps</a> <a class=gb1 href="https://play.google.com/?hl=en&tab=w8">Play</a> <a class=gb1 href="http://www.youtube.com/?tab=w1">YouTube</a> <a class=gb1 href="http://news.google.com/nwshp?hl=en&tab=wn">News</a> <a class=gb1 href="https://mail.google.com/mail/?tab=wm">Gmail</a> <a class=gb1 href="https://drive.google.com/?tab=wo">Drive</a> <a class=gb1 style="text-decoration:none" href="http://www.google.com/intl/en/options/"><u>More</u> &raquo;</a></nobr></div><div id=guser width=100%><nobr><span id=gbn class=gbi></span><span id=gbf class=gbf></span><span id=gbe></span><a href="http://www.google.com/history/optout?hl=en" class=gb4>Web History</a> | <a  href="/preferences?hl=en" class=gb4>Settings</a> | <a target=_top id=gb_70 href="https://accounts.google.com/ServiceLogin?hl=en&continue=http://www.google.com/" class=gb4>Sign in</a></nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div>  </div><center><br clear="all" id="lgpd"><div id="lga"><img alt="Google" height="95" src="/images/srpr/logo9w.png" width="269" id="hplogo" onload="window.lol&&lol()" style="padding:28px 0 14px"><br><br></div><form action="/search" name="f"><table cellpadding="0" cellspacing="0"><tr valign="top"><td width="25%">&nbsp;</td><td align="center" nowrap=""><input name="ie" value="ISO-8859-1" type="hidden"><input value="en" name="hl" type="hidden"><input name="source" type="hidden" value="hp"><div class="ds" style="height:32px;margin:4px 0"><input autocomplete="off" class="lst" value="" title="Google Search" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top"></div><br style="line-height:0"><span class="ds"><span class="lsbb"><input class="lsb" value="Google Search" name="btnG" type="submit"></span></span><span class="ds"><span class="lsbb"><input class="lsb" value="I'm Feeling Lucky" name="btnI" type="submit" onclick="if(this.form.q.value)this.checked=1; else top.location='/doodles/'"></span></span></td><td class="fl sblc" align="left" nowrap="" width="25%"><a href="/advanced_search?hl=en&amp;authuser=0">Advanced search</a><a href="/language_tools?hl=en&amp;authuser=0">Language tools</a></td></tr></table><input type="hidden" id="gbv" name="gbv" value="1"></form><div id="gac_scont"></div><div style="font-size:83%;min-height:3.5em"><br><div id="prm"><font id="hpplink" size="-1" style="behavior:url(#default#userdata);display:none"><span style="color:red"></span>Your idea just might change the world. <a href="https://www.googlesciencefair.com/?utm_source=hpp&amp;utm_medium=desktop&amp;utm_content=hppen&amp;utm_campaign=GSF2014" onclick="google.promos&&google.promos.link&& google.promos.link.cl()">Enter Google Science Fair 2014</a></font><br><br><br><script type="text/javascript">(function(){var e={ACCEPT:"a",LIMITED_ACCEPT:"l",CANCEL:"c",DISMISS:"d",CLICK:"h",IMPRESSION:"i",NO_THANKS:"n",X_BUTTON:"x",MGMHP_ACCEPT:"ma",MGMHP_CANCEL:"mc",MGMHP_IMPRESSION:"mi",MGMHPPD_ACCEPT:"pa",MGMHPPD_CANCEL:"pc",MGMHPPD_IMPRESSION:"pi",MGMHPPD_NO_THANKS:"pn",MGMHPPD_NO_BUTTON:"px",MGMHPPD_DISMISS:"pd",PUSHDOWN_ACCEPT:"gpa",PUSHDOWN_IMPRESSION:"gpi",PUSHDOWN_NO_THANKS:"gpn",PUSHDOWN_X_BUTTON:"gpx",PUSHDOWN_DISMISS:"gpd"};var f,g,h=0,k;google.promos=google.promos||{};google.promos.link=google.promos.link||{};google.promos.link.getExtraLogData_=function(b){var a=document.getElementById(k);if(a){var c=g+"_upccb",d=parseInt(window.gbar.up.gpd(a,c)||0,10);d++;h++;window.gbar.up.spd(a,c,d);a=[["upcc",h].join("="),["upccb",d].join("=")];b&&a.push(b);return a.join("&")}};google.promos.link.cl=function(b){try{window.gbar.up.sl(g,f,e.CLICK,google.promos.link.getExtraLogData_(b),1)}catch(a){google.ml(a,!1,{cause:f+"_CL"})}};function l(){var b=["gpd","spd","sl"];if(!window.gbar||!window.gbar.up)return!1;for(var a=0,c;c=b[a];a++)if(!(c in window.gbar.up))return!1;return!0}google.promos.link.init=function(b,a,c){try{if(l()){g=b;f=a;k=c;var d=document.getElementById(k);d&&(d.style.display="",window.gbar.up.sl(g,f,e.IMPRESSION))}else google.ml(Error("apa"),!1,{cause:a+"_INIT"})}catch(m){google.ml(m,!1,{cause:f+"_INIT"})}};})();</script><script>(function(){var sourceWebappPromoID=4076556;var payloadType=3;window.gbar&&gbar.up&&gbar.up.r&&gbar.up.r(payloadType,function(show){if (show){google.promos.link.init(sourceWebappPromoID,payloadType,'hpplink');}
});})();</script></div></div><span id="footer"><div style="font-size:10pt"><div id="fll" style="margin:19px auto;text-align:center"><a href="/intl/en/ads/">Advertising&nbsp;Programs</a><a href="/services/">Business Solutions</a><a href="https://plus.google.com/116899029375914044550" rel="publisher">+Google</a><a href="/intl/en/about.html">About Google</a></div></div><p style="color:#767676;font-size:8pt">&copy; 2013 - <a href="/intl/en/policies/">Privacy & Terms</a></p></span></center><div id=xjsd></div><div id=xjsi data-jiis="bp"><script>if(google.y)google.y.first=[];(function(){function b(a){window.setTimeout(function(){var c=document.createElement("script");c.src=a;document.getElementById("xjsd").appendChild(c)},0)}google.dljp=function(a){google.xjsu=a;b(a)};google.dlj=b;})();
if(!google.xjs){window._=window._||{};window._._DumpException=function(e){throw e};if(google.timers&&google.timers.load.t){google.timers.load.t.xjsls=new Date().getTime();}google.dljp('/xjs/_/js/k\x3dxjs.hp.en_US.-XqH2jhAx8Y.O/m\x3dsb_he,pcc/rt\x3dj/d\x3d1/sv\x3d1/rs\x3dAItRSTMPrrl3KxUDBBBsP87paJg779ovYA');google.xjs=1;}google.pmc={"sb_he":{"agen":true,"cgen":true,"client":"heirloom-hp","dh":true,"ds":"","eqch":true,"fl":true,"host":"google.com","jsonp":true,"msgs":{"dym":"Did you mean:","lcky":"I\u0026#39;m Feeling Lucky","lml":"Learn more","oskt":"Input tools","psrc":"This search was removed from your \u003Ca href=\"/history\"\u003EWeb History\u003C/a\u003E","psrl":"Remove","sbit":"Search by image","srch":"Google Search"},"ovr":{},"pq":"","qcpw":false,"scd":10,"sce":5,"stok":"Pt_geZZSOq5buz2dzeVrHvTtYic"},"hp":{},"pcc":{}};google.y.first.push(function(){if(google.med){google.med('init');google.initHistory();google.med('history');}});if(google.j&&google.j.en&&google.j.xi){window.setTimeout(google.j.xi,0);}</script></div><script>(function(){if(google.timers&&google.timers.load.t){var b,c,d,e,g=function(a,f){a.removeEventListener?(a.removeEventListener("load",f,!1),a.removeEventListener("error",f,!1)):(a.detachEvent("onload",f),a.detachEvent("onerror",f))},h=function(a){e=(new Date).getTime();++c;a=a||window.event;a=a.target||a.srcElement;g(a,h)},k=document.getElementsByTagName("img");b=k.length;for(var l=c=0,m;l<b;++l)m=k[l],m.complete||"string"!=typeof m.src||!m.src?++c:m.addEventListener?(m.addEventListener("load",h,!1),m.addEventListener("error",
h,!1)):(m.attachEvent("onload",h),m.attachEvent("onerror",h));d=b-c;var n=function(){if(google.timers.load.t){google.timers.load.t.ol=(new Date).getTime();google.timers.load.t.iml=e;google.kCSI.imc=c;google.kCSI.imn=b;google.kCSI.imp=d;void 0!==google.stt&&(google.kCSI.stt=google.stt);google.csiReport&&google.csiReport()}};window.addEventListener?window.addEventListener("load",n,!1):window.attachEvent&&
window.attachEvent("onload",n);google.timers.load.t.prt=e=(new Date).getTime()};})();
</script></body></html>
*** END ***

Open in new window

So, the code works.