Android studio : using cookies -
i'm attempting use cookies (with no success), can it's value fine sending whole different story.
i'm using 2 asynctask classes, 2 buttons , 2 textboxes. after clicking first button first class accesses url , acquires cookie saving first textbox string value (works fine). second click supposed cookie textbox , send second url show online message (does not work).
the code 2 buttons:
button1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //executing first asyntask new downloadwebpagetask().execute(firstlink); } }); button2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string[] ar= new string[2]; //save value of second link , cookie value in array ar[0]=secondlink; ar[1]= string.valueof(textbox1.gettext()); //executing second asyntask new showc().execute(ar); } });
the asynctasks :
//first asynctask private class downloadwebpagetask extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { url url = null; urlconnection conn=null; string output=""; try { url = new url(urls[0]); conn = url.openconnection(); conn.connect(); return conn.getheaderfield("set-cookie"); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return "error"; } // onpostexecute displays results of asynctask. @override protected void onpostexecute(string result) { //store cookie value string, : phpsessid=....; textbox1.settext(result); } } //second asynctask private class showc extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { url url = null; httpurlconnection conn=null; string output=""; try { url = new url(urls[0]); conn = (httpurlconnection) url.openconnection(); conn.setrequestproperty("set-cookie", urls[1]); conn.connect(); bufferedreader br = new bufferedreader(new inputstreamreader((conn.getinputstream()))); output=br.readline(); return output+" "+conn.getheaderfield("set-cookie"); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return "error"; } // onpostexecute displays results of asynctask. @override protected void onpostexecute(string result) { text2.settext(result); } } //----------------------
setting cookie value in second link not work , returns different cookie when checking value again..should note using cookies android poorly documented , documentations based on using deprecated httpclient instead of urlconnection, based technique example.
in second asynctask, use "cookie" instead of "set-cookie" in "setrequestproperty" function.
private class showc extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { url url = null; httpurlconnection conn=null; string output=""; try { url = new url(urls[0]); conn = (httpurlconnection) url.openconnection(); // modification conn.setrequestproperty("cookie", urls[1]); conn.connect(); bufferedreader br = new bufferedreader(new inputstreamreader((conn.getinputstream()))); output=br.readline(); return output+" "+conn.getheaderfield("set-cookie"); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return "error"; } // onpostexecute displays results of asynctask. @override protected void onpostexecute(string result) { text2.settext(result); } }
the above method hard , tough state management. can use simple method given on link.
Comments
Post a Comment