java - HTTP URL connection: display specific line of website source code -
im building first application , im kind of new but:
i'm sending http request using httpurlconnection. here's activity code:
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; import java.net.urlconnection; import android.app.activity; import android.os.asynctask; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; public class httpgetservletactivity3 extends activity implements onclicklistener { button button; textview outputtext; public static final string url = "url"; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); findviewsbyid(); button.setonclicklistener(this); } private void findviewsbyid() { button = (button) findviewbyid(r.id.button); outputtext = (textview) findviewbyid(r.id.outputtxt); } public void onclick(view view) { getxmltask task = new getxmltask(); task.execute(new string[] { url }); } private class getxmltask extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { string output = null; (string url : urls) { output = getoutputfromurl(url); } return output; } private string getoutputfromurl(string url) { stringbuffer output = new stringbuffer(""); try { inputstream stream = gethttpconnection(url); bufferedreader buffer = new bufferedreader( new inputstreamreader(stream)); string s = ""; while ((s = buffer.readline()) != null) output.append(s); } catch (ioexception e1) { e1.printstacktrace(); } return output.tostring(); } // makes httpurlconnection , returns inputstream private inputstream gethttpconnection(string urlstring) throws ioexception { inputstream stream = null; url url = new url(urlstring); urlconnection connection = url.openconnection(); try { httpurlconnection httpconnection = (httpurlconnection) connection; httpconnection.setrequestmethod("get"); httpconnection.connect(); if (httpconnection.getresponsecode() == httpurlconnection.http_ok) { stream = httpconnection.getinputstream(); } } catch (exception ex) { ex.printstacktrace(); } return stream; } @override protected void onpostexecute(string output) { outputtext.settext(output); } } }
this code me display website's source code, , it's working !! if want specific line of source code? example line 60? tried change following buuferedreader block:
bufferedreader buffer = new bufferedreader( new inputstreamreader(stream)); string s = ""; while ((s = buffer.readline()) != null) output.append(s);
and wrote following linereader instead didnt work:
linenumberreader lnr = new linenumberreader(new inputstreamreader(stream)); string s = ""; if (lnr.getlinenumber() == 60){ output.append(s); }
is way or missing ?
you can use setlinenumber method;
lnr.setlinenumber(60)
and then, can read line!
Comments
Post a Comment