Getting the content of a variable from php to android and display it using Toast class -
i want content of variable php
android , display using toast class. wrote following codes , if 1 or both fields empty, register.php
file prints $isempty
, java code should content of $isempty
variable , if(text == "isempty")
result toast class should display r.string.empty
. program using following codes can not display r.string.empty
using toast class.
public class mainactivity extends activity { edittext username, password; button register; string name, pass; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); username = (edittext) findviewbyid(r.id.edittext); password= (edittext) findviewbyid(r.id.edittext2); register= (button) findviewbyid(r.id.register); register.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { try{ // call gettext method make post method call gettext(); } catch(exception ex) { } } }); } public void gettext() throws unsupportedencodingexception { // user defined values name = username.gettext().tostring(); pass = password.gettext().tostring(); // create data variable sent values server string data = urlencoder.encode("username", "utf-8") + "=" + urlencoder.encode(name, "utf-8"); data += "&" + urlencoder.encode("password", "utf-8") + "=" + urlencoder.encode(pass, "utf-8"); string text = ""; bufferedreader reader=null; // send data try { // defined url send data url url = new url("http://127.0.0.1:8080/apps/register.php"); // send post data request urlconnection conn = url.openconnection(); conn.setdooutput(true); outputstreamwriter wr = new outputstreamwriter(conn.getoutputstream()); wr.write( data ); wr.flush(); // server response reader = new bufferedreader(new inputstreamreader(conn.getinputstream())); stringbuilder sb = new stringbuilder(); string line = null; // read server response while((line = reader.readline()) != null) { // append server response in string sb.append(line + "\n"); } text = sb.tostring(); } catch(exception ex) { } { try { reader.close(); } catch(exception ex) {} } // show response on activity if(text == "isempty") { string isempty = (string)gettext(r.string.empty); toast.maketext(getapplicationcontext(), isempty, toast.length_long).show(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; }}
and
<?php if($_server["request_method"] == "post") { $username = urldecode($_post['username']); $login = urldecode($_post['login']); if(empty($username) || empty($login)) { $isempty = "isempty"; print "$isempty"; } }?>
using
if(text == "isempty")
incorrect
you have use
if(text.equals("isempty"))
in java
Comments
Post a Comment