java - Use Http POST and GET to navigate a website -
i want android app establish connection website , through code navigate website in background. understand, first step html source code of site wish navigate managed through code:
public class httptest extends activity { private textview tvcode; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.http_layout); tvcode = (textview)findviewbyid(r.id.tvhtmlcode); string s = null; try { gethtmlsourcecode html = new gethtmlsourcecode(); html.execute("http://www.youtube.com"); s = html.get(); } catch (exception e) { e.printstacktrace(); tvcode.settext("error"); } if (s != null) tvcode.settext(s); } private class gethtmlsourcecode extends asynctask<string, integer, string> { @override protected string doinbackground(string... params) { url url = null; httpurlconnection conn = null; string content = ""; try { url = new url(params[0]); conn = (httpurlconnection)url.openconnection(); inputstream in = conn.getinputstream(); int data = in.read(); while (data != -1) { char c = (char) data; data = in.read(); content += c; } } catch (exception e) { e.printstacktrace(); return "error"; } { conn.disconnect(); return content; } } }
}
(youtube used example).
after getting source code youtube.com, want app input in search box , click search button.
understand need send post request youtube fill search box, post press button , get html source code of page search result. deprecation of httpclient , httppost classes these problems appear have been solved, limited english vocabulary , general ignorance on subject make difficult find solution myself.
can help?
try using following code
import java.io.bytearrayoutputstream; import java.io.ioexception; import org.apache.http.httpresponse; import org.apache.http.httpstatus; import org.apache.http.statusline; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient; import android.os.asynctask; public class request extends asynctask<string, string, string>{ private httpclient httpclient = new defaulthttpclient(); private httpresponse response; private string responsestring ; @override protected string doinbackground(string... uri) { statusline statusline = response.getstatusline(); try { response = httpclient.execute(new httpget(uri[0])); statusline = response.getstatusline(); if(statusline.getstatuscode() == httpstatus.sc_ok){ bytearrayoutputstream out = new bytearrayoutputstream(); response.getentity().writeto(out); responsestring = out.tostring(); out.close(); } else{ response.getentity().getcontent().close(); throw new ioexception(statusline.getreasonphrase()); } } catch (clientprotocolexception e) { } catch (ioexception e) { } return responsestring; } @override protected void onpostexecute(string result) { super.onpostexecute(result); //to } }
to make request, use following code
new requesttask().execute("http://yourwebsite.com");
Comments
Post a Comment