android - if condition doesnt work properly always go in false block -
hi following code trying server responce if status equal success show success message , if failed show failed time show failed message why happen.please give me solution of it. verify.java dont know execute true or false block of if condition
public class verifyotp extends asynctask<string, string, string> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(verifyotp.this); pdialog.setmessage("loading...."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } @override protected string doinbackground(string... strings) { try { list<namevaluepair> param = new arraylist<namevaluepair>(); param.add(new basicnamevaluepair("edituser", uid)); param.add(new basicnamevaluepair("editotp", abc)); jsonobject json = jpar.makehttprequest(url_verify_otp, "post", param); string sts=json.getstring(tag_status); log.v(tag,""+json.getstring(tag_status)); if(sts=="succuss") { log.v(tag,"in success block"); lflag=true; } else { lflag=false; } }catch(exception e){ e.printstacktrace(); log.v(tag, "exception @ end :" + e.tostring()); //log.e("tag", "error......!recoverit"); } return null; } @override protected void onpostexecute(string result) { super.onpostexecute(result); if(lflag==true) { toast.maketext(verifyotp.this, "successsss", toast.length_short).show(); }else { toast.maketext(verifyotp.this, "faileddddd", toast.length_short).show(); } pdialog.hide(); pdialog.dismiss(); } } it doesnt set lflag true show failed message.the problem follow
public class jsonparser { static inputstream is; static jsonobject jobj; static string json; static jsonarray jarr; private static final string tag = "myappsurun"; public static string alternatejsonstring = ""; public static jsonarray alternatejsonarray = null; // constructor public jsonparser() { = null; jobj = null; json = ""; jarr = null; } // function json url // making http post or mehtod public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request try { // check request method if(method.equalsignorecase("post")){ // request method post // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); log.v(tag, "param string :" + url); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); log.v(tag,"to end of if"); }else if(method.equalsignorecase("get")){ // request method defaulthttpclient httpclient = new defaulthttpclient(); string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; log.v(tag,"param string :"+url); httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); log.v(tag,"to end of else"); } } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader(is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); json = sb.tostring(); log.v(tag,"js_parser : server response -"+json); } catch (exception e) { log.v(tag,"error in getting json string response : further try given use json string "+e.tostring()); } // try parse string json object/array , string try { if(json != null) { alternatejsonstring = json; jobj = new jsonobject(json); log.v(tag,"js_parser : json object created use object instance"); } else { log.v(tag,"js_parser : json string blank trying 1 more time parse json string"); } } catch (exception e) { try{ if(json != null) { log.v(tag,"js_parser : object parsing failed trying array if no more logs followed \" js_parser \" use jsonparser.alternatejsonarray json instance. "); alternatejsonarray = new jsonarray(json); } else { log.v(tag,"js_parser : json string blank trying 1 last time parse json string if no more logs followed \" js_parser \" use jsonparser.alternatejsonstring json instance"); } } catch(exception e1) { if(json !=null) { log.v(tag, "js_parser : array parsing failed if no more logs followed \" js_parser \" use jsonparser.alternatejsonstring backup json string results"); } else { log.v(tag, "js_parser : final try failed confirm server log request error or follow following log trail"+e1); } } } // return json string return jobj; } this jsonparser file if entered wrong otp display me following response
1843-1904/com.example.surun.suruninfocore v/myappsurun﹕ end of if 10-01 11:19:11.207 1843-1904/com.example.surun.suruninfocore v/myappsurun﹕ js_parser : server response - 10-01 11:19:11.213 1843-1904/com.example.surun.suruninfocore v/myappsurun﹕ js_parser : object parsing failed trying array if no more logs followed " js_parser " use jsonparser.alternatejsonarray json instance. 10-01 11:19:11.213 1843-1904/com.example.surun.suruninfocore v/myappsurun﹕ js_parser : array parsing failed if no more logs followed " js_parser " use jsonparser.alternatejsonstring backup json string results 10-01 11:19:11.224 1843-1904/com.example.surun.suruninfocore w/system.err﹕ java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string org.json.jsonobject.getstring(java.lang.string)' on null object reference 10-01 11:19:11.227 1843-1904/com.example.surun.suruninfocore w/system.err﹕ @ com.example.surun.suruninfocore.verifyotp$verifyotp.doinbackground(verifyotp.java:85) 10-01 11:19:11.227 1843-1904/com.example.surun.suruninfocore w/system.err﹕ @ com.example.surun.suruninfocore.verifyotp$verifyotp.doinbackground(verifyotp.java:68)
use equals instead of == .try way .i hope works
in java, 1 of common mistakes meet using == compare strings. have remember, == compares object references, not content.
string1.equals(string target) compares 2 strings based off of actual characters in strings.
good practice/proper way
if(sts.equals("succuss")) { log.v(tag,"in success block"); lflag=true; }
Comments
Post a Comment