php - Comparing result from the server with a string in java 2ME -
i'm working on java 2me app submit data mysql database (am running php scripting language), having problem compare string result comes server. want compare string has been print php eg. echo "ok"; string string str = "ok" within java 2me codes. can me this? here codes
register.java
public void register() { string result = null; posthttp post = new posthttp(); post.setdocument("birth"); post.setpath("/notification/"); //set parameters post.setparameter("firstname", getchildfirstname().getstring()); post.setparameter("middlename", getchildmiddlename().getstring()); post.setparameter("lastname", getchildlastname().getstring()); post.setparameter("gender", getchildgender().getstring(getchildgender().getselectedindex()));/***************/ post.setparameter("childdob", getchilddob().getdate().tostring());/*******************/ post.setparameter("birthtype", getbirthtype().getstring(getbirthtype().getselectedindex())); post.setparameter("weight", getbornwithweight().getstring()); post.setparameter("mother_fname", getmotherfirstname().getstring()); post.setparameter("mother_mname", getmothermiddlename().getstring()); post.setparameter("mother_lname", getmotherlastname().getstring()); post.setparameter("birthplace", getplaceofbirth().getstring()); post.setparameter("residence", getresidence().getstring()); post.setparameter("residence", getresidence().getstring()); result = post.submit(); string x = "ok"; if(result.compareto(x)){ this.append("equal strings"); }else{ this.append("not equal"); } } posthttp.java
import java.io.*; import javax.microedition.io.*; public class posthttp { private string protocol; private string host; private string path; //path file private string document; // controller function private string data; // post data public posthttp() { // set defaults protocol = "http://"; host = "localhost/badis/index.php"; path = "/notification/"; document = "trial"; data = ""; } public void setprotocol(string p) { protocol = p; } public void sethost(string h) { host = h; } public void setpath(string p) { path = p; } public void setdocument(string d) { document = d; } public void setparameter(string parameter, string value) { if (data.length() > 0) { data = data + "&"; } data = data + parameter + "=" + value; } httpconnection setupconnection(string uri) throws ioexception { httpconnection connection = null; connection = (httpconnection) connector.open(uri, connector.read_write); connection.setrequestmethod(httpconnection.post); connection.setrequestproperty("content-type", "application/x-www-form-urlencoded"); connection.setrequestproperty("host", host); return (connection); } void sendpostdata(httpconnection connection, string postdata) throws ioexception { outputstream outputstream = null; outputstream = connection.openoutputstream(); outputstream.write(postdata.getbytes()); outputstream.close(); } string readresult(httpconnection connection) throws ioexception { inputstreamreader inputstream; string result = ""; final int arbitrarybuffersize = 100; final int endofdata = -1; char[] characterbuffer = new char[arbitrarybuffersize]; int bytesread; inputstream = new inputstreamreader(connection.openinputstream()); bytesread = inputstream.read(characterbuffer); while (bytesread != endofdata) { result = result + new string(characterbuffer); bytesread = inputstream.read(characterbuffer); } inputstream.close(); return (result); } public string submit() { httpconnection connection; string result; try { connection = setupconnection(protocol + host + path + document); sendpostdata(connection, data); result = readresult(connection); connection.close(); } catch (ioexception ioe) { result = "ioexception!"; } return (result); } } result
not equal php script
public function birth() { echo "ok"; } am using codeigniter 2.0.1, if helps.
thanx in advance.. cheers
lets absolutely clear this. if following appending "not equal":
string x = "ok"; if (result.equals(x)) { this.append("equal strings"); } else{ this.append("not equal"); } it because result not equal "ok". there can no doubt this.
i suspect 1 of following:
there leading or trailing whitespace on
resultstring; i.e. space, tab or cr or nl character can't see when output string console.possibly, first character 0 ('0') character instead of capital oh ('o').
possibly, case different.
one way bottom of take 2 strings apart , compare them character character; e.g.
string s = "ok"; if (result.equals(x)) { this.append("equal strings"); } else if (x.length() != result.length()) { this.append("lengths different"); } else if (x.charat(0) != result.charat(0)) { this.append("character 0 different"); } else if (x.charat(1) != result.charat(1)) { this.append("character 1 different"); } else { this.append("the universe being swallowed giant squid"); }
Comments
Post a Comment