java - What's wrong with this code in Android (PHP + MySQL)? -


i have problem application. i'm trying make register form in android, have problems.

when i'm running app, great, when try save data, shows me error message unknown status!, don't know what's wrong!

could you, please, me.

below code:

ps: tried stealz's answer , receive error username exist, table has 1 insert.


nota_spinner.xml

<tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tablelayout1" android:layout_width="fill_parent" android:layout_height="fill_parent">  <tablerow   android:id="@+id/tablerow1"   android:layout_width="wrap_content"   android:layout_height="wrap_content" >   <textview     android:id="@+id/textview1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:gravity="center"     android:text="register form "     android:layout_span="1"     android:textappearance="?android:attr/textappearancelarge" /> </tablerow>  <view android:layout_height="1dip" android:background="#cccccc" />    <tablelayout      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_weight="0.1"     android:orientation="horizontal">    <textview       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="input username :"       android:textappearance="?android:attr/textappearancemedium" />    <edittext       android:id="@+id/txtusername"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:ems="10" >   </edittext>    <textview       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="input password/confirm password :"       android:textappearance="?android:attr/textappearancemedium" />     <tablerow>   <edittext       android:id="@+id/txtpassword"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:ems="6"       android:inputtype="textpassword" >   </edittext>   <textview       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text=" again " />    <edittext       android:id="@+id/txtconpassword"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:ems="7"       android:inputtype="textpassword" >   </edittext>   </tablerow>    <textview       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="input name :"       android:textappearance="?android:attr/textappearancemedium" />    <edittext       android:id="@+id/txtname"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:ems="10" >   </edittext>    <textview       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="input email :"       android:textappearance="?android:attr/textappearancemedium" />    <edittext       android:id="@+id/txtemail"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:inputtype="textemailaddress"       android:ems="10" >   </edittext>    <textview       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="input tel :"       android:textappearance="?android:attr/textappearancemedium" />    <edittext       android:id="@+id/txttel"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:inputtype="phone"       android:ems="10" >   </edittext>  <button  android:id="@+id/btnsave" android:layout_width="wrap_content"  android:layout_height="wrap_content" android:text="save" />   </tablelayout > <view     android:layout_height="1dip"     android:background="#cccccc" />  <linearlayout   android:id="@+id/linearlayout1"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:padding="5dip" >      <textview         android:id="@+id/textview2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="by.. thaicreate.com" />  </linearlayout>  </tablelayout> 

class.java

public class class extends activity {   @suppresslint("newapi") @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.nota_spinner);      // permission strictmode     if (android.os.build.version.sdk_int > 9) {         strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build();         strictmode.setthreadpolicy(policy);     }      // btnsave     final button btnsave = (button) findviewbyid(r.id.btnsave);     // perform action on click     btnsave.setonclicklistener(new view.onclicklistener() {         public void onclick(view v) {             if(savedata())             {                 // when save complete             }         }        });  }  public boolean savedata() {      // txtusername,txtpassword,txtname,txtemail,txttel     final edittext txtusername = (edittext)findviewbyid(r.id.txtusername);      final edittext txtpassword = (edittext)findviewbyid(r.id.txtpassword);      final edittext txtconpassword = (edittext)findviewbyid(r.id.txtconpassword);      final edittext txtname = (edittext)findviewbyid(r.id.txtname);      final edittext txtemail = (edittext)findviewbyid(r.id.txtemail);      final edittext txttel = (edittext)findviewbyid(r.id.txttel);            // dialog         final alertdialog.builder ad = new alertdialog.builder(this);          ad.settitle("error! ");         ad.seticon(android.r.drawable.btn_star_big_on);          ad.setpositivebutton("close", null);          // check username         if(txtusername.gettext().length() == 0)         {             ad.setmessage("please input [username] ");             ad.show();             txtusername.requestfocus();             return false;         }         // check password         if(txtpassword.gettext().length() == 0 || txtconpassword.gettext().length() == 0 )         {             ad.setmessage("please input [password/confirm password] ");             ad.show();             txtpassword.requestfocus();             return false;         }         // check password , confirm password (match)              if(!txtpassword.gettext().tostring().equals(txtconpassword.gettext().tostring()))         {             ad.setmessage("password , confirm password not match! ");             ad.show();             txtconpassword.requestfocus();             return false;         }         // check name         if(txtname.gettext().length() == 0)         {             ad.setmessage("please input [name] ");             ad.show();             txtname.requestfocus();             return false;         }         // check email         if(txtemail.gettext().length() == 0)         {             ad.setmessage("please input [email] ");             ad.show();             txtemail.requestfocus();             return false;         }         // check tel         if(txttel.gettext().length() == 0)         {             ad.setmessage("please input [tel] ");             ad.show();             txttel.requestfocus();             return false;         }           string url = "blahblah/saveadddata.php";          list<namevaluepair> params = new arraylist<namevaluepair>();         params.add(new basicnamevaluepair("susername",    txtusername.gettext().tostring()));         params.add(new basicnamevaluepair("spassword", txtpassword.gettext().tostring()));         params.add(new basicnamevaluepair("sname", txtname.gettext().tostring()));         params.add(new basicnamevaluepair("semail", txtemail.gettext().tostring()));         params.add(new basicnamevaluepair("stel", txttel.gettext().tostring()));          /** result server (return json code)          * statusid = ? [0=failed,1=complete]          * error    = ? [on case error return custom error message]          *           * eg save failed = {"statusid":"0","error":"email exists!"}          * eg save complete = {"statusid":"1","error":""}          */           string resultserver  = gethttppost(url,params);          /*** default value ***/         string strstatusid = "0";         string strerror = "unknow status!";          jsonobject c;         try {             c = new jsonobject(resultserver);             strstatusid = c.getstring("statusid");             strerror = c.getstring("error");         } catch (jsonexception e) {             // todo auto-generated catch block             e.printstacktrace();         }          // prepare save data         if(strstatusid.equals("0"))         {             ad.setmessage(strerror);             ad.show();         }         else         {             toast.maketext(nota.this, "save data successfully", toast.length_short).show();             txtusername.settext("");             txtpassword.settext("");             txtconpassword.settext("");             txtname.settext("");             txtemail.settext("");             txttel.settext("");         }       return true; }   public string gethttppost(string url,list<namevaluepair> params) {     stringbuilder str = new stringbuilder();     httpclient client = new defaulthttpclient();     httppost httppost = new httppost(url);      try {         httppost.setentity(new urlencodedformentity(params));         httpresponse response = client.execute(httppost);         statusline statusline = response.getstatusline();         int statuscode = statusline.getstatuscode();         if (statuscode == 200) { // status ok             httpentity entity = response.getentity();             inputstream content = entity.getcontent();             bufferedreader reader = new bufferedreader(new inputstreamreader(content));             string line;             while ((line = reader.readline()) != null) {                 str.append(line);             }         } else {             log.e("log", "failed download result..");         }     } catch (clientprotocolexception e) {         e.printstacktrace();     } catch (ioexception e) {         e.printstacktrace();     }     return str.tostring(); }   } 

and php file saveadddata.php

<? $objconnect = mysql_connect("localhost","root",""); $objdb = mysql_select_db("retailer");  /*** sample      $_post["susername"] = "a";     $_post["spassword"] = "b";     $_post["sname"] = "c";     $_post["semail"] = "d";     $_post["stel"] = "e"; */  $strusername = $_post["susername"]; $strpassword = $_post["spassword"]; $strname = $_post["sname"]; $stremail = $_post["semail"]; $strtel = $_post["stel"];  /*** check username exists ***/ $strsql = "select * member username = '".$strusername."' "; $objquery = mysql_query($strsql); $objresult = mysql_fetch_array($objquery); if($objresult) {     $arr['statusid'] = "0";      $arr['error'] = "username exists!";      echo json_encode($arr);     exit(); }  /*** check email exists ***/ $strsql = "select * member email = '".$stremail."' "; $objquery = mysql_query($strsql); $objresult = mysql_fetch_array($objquery); if($objresult) {     $arr['statusid'] = "0";      $arr['error'] = "email exists!";         echo json_encode($arr);     exit(); }  /*** insert ***/ $strsql = "insert member (username,password,name,email,tel)      values (         '".$strusername."',         '".$strpassword."',         '".$strname."',         '".$stremail."',         '".$strtel."'         )     ";  $objquery = mysql_query($strsql); if(!$objquery) {     $arr['statusid`enter code here`'] = "0";      $arr['error'] = "cannot save data!";     } else {     $arr['statusid'] = "1";      $arr['error'] = "";  }  /**     $arr['statusid'] // (0=failed , 1=complete)     $arr['error'] // error message */  mysql_close($objconnect);  echo json_encode($arr); ?> 

from see, getting error "unknown status" because getting response statusid = 0. , might according php @ multiple places(username exist,email exists).

what suggest give different statusid different errors username give id=11 exists, email exists give 22, , check on android side error, can give specific message error, saying unknown error, because know error getting.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -