java - how send request from android form to localhost URL??? -


i want send android form data specific url (like login.php page "this page of website verification of form data db saved record")

see tutorial , done

http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/

you need 2 classes 1. activity 2. json parser

here sample code 2 of them, can modify per own needs

public class jsonparser {      static inputstream = null;     static jsonobject jobj = null;     static string json = "";     static jsonarray jarr = null;      // constructor     public jsonparser() {      }      // function json url     // making http post or method     public jsonobject makehttprequest(string url, string method,             list<namevaluepair> params) {          // making http request         try {              // check request method             if(method == "post"){                 // request method post                 // defaulthttpclient                  defaulthttpclient httpclient = new defaulthttpclient();                 httppost httppost = new httppost(url);                 httppost.setentity(new urlencodedformentity(params));                  httpresponse httpresponse = httpclient.execute(httppost);                 httpentity httpentity = httpresponse.getentity();                 = httpentity.getcontent();              }else if(method == "get"){                 // request method                 defaulthttpclient httpclient = new defaulthttpclient();                 string paramstring = urlencodedutils.format(params, "utf-8");                 url += "?" + paramstring;                 httpget httpget = new httpget(url);                  httpresponse httpresponse = httpclient.execute(httpget);                 httpentity httpentity = httpresponse.getentity();                 = httpentity.getcontent();             }                    } catch (unsupportedencodingexception e) {             e.printstacktrace();         } catch (clientprotocolexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }          try {             bufferedreader reader = new bufferedreader(new inputstreamreader(                     is, "utf-8"), 8);             stringbuilder sb = new stringbuilder();             string line = null;             while ((line = reader.readline()) != null) {                 sb.append(line + "\n");             }             is.close();             json = sb.tostring();         } catch (exception e) {             log.e("buffer error", "error converting result " + e.tostring());          }         log.i("php error", "["+json+"]");         // try parse string json object         try {             jobj = new jsonobject(json);         } catch (jsonexception e) {             log.e("json parser", "error parsing data " + e.getmessage() + json);         }          // return json string         return jobj;      }      public jsonarray getallmessages(string url, string method,             list<namevaluepair> params) {          // making http request         try {              // check request method             if(method == "post"){                 // request method post                 // defaulthttpclient                  defaulthttpclient httpclient = new defaulthttpclient();                 httppost httppost = new httppost(url);                 httppost.setentity(new urlencodedformentity(params));                  httpresponse httpresponse = httpclient.execute(httppost);                 httpentity httpentity = httpresponse.getentity();                 = httpentity.getcontent();              }else if(method == "get"){                 // request method                 defaulthttpclient httpclient = new defaulthttpclient();                 string paramstring = urlencodedutils.format(params, "utf-8");                 url += "?" + paramstring;                 httpget httpget = new httpget(url);                  httpresponse httpresponse = httpclient.execute(httpget);                 httpentity httpentity = httpresponse.getentity();                 = httpentity.getcontent();             }                    } catch (unsupportedencodingexception e) {             e.printstacktrace();         } catch (clientprotocolexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }          try {             bufferedreader reader = new bufferedreader(new inputstreamreader(                     is, "utf-8"), 8);             stringbuilder sb = new stringbuilder();             string line = null;             while ((line = reader.readline()) != null) {                 sb.append(line + "\n");             }             is.close();             json = sb.tostring();         } catch (exception e) {             log.e("buffer error", "error converting result " + e.tostring());          }         log.i("php error", "["+json+"]");         // try parse string json object         try {             jarr = new jsonarray(json);         } catch (jsonexception e) {             log.e("json parser", "error parsing data " + e.getmessage() + json);         }          // return json string         return jarr;      } } 

activity

public class home extends activity {      private progressdialog pdialog;      jsonparser jsonparser = new jsonparser();     string message;      private static string url_addmessage = "http://www.yourdomain.com/addmessage.php";      private static final string tag_success = "success";      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_home);          final edittext et = (edittext)findviewbyid(r.id.edittext1);          button b1 = (button)findviewbyid(r.id.button1);         button b2 = (button)findviewbyid(r.id.button2);          b1.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // todo auto-generated method stub                 try{                         message = et.gettext().tostring();                          log.i("message posted", message);                          new registerme().execute();                 }catch(exception e){                     log.e("exception while getting message", e.getmessage().tostring());                 }                  }         });          b2.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // todo auto-generated method stub                 startactivity(new intent(home.this, getmessage.class));             }         });       }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.activity_home, menu);         return true;     }      class registerme extends asynctask<string, string, string>{         string message=null;          public boolean workdone(string message){              boolean x=false;             if(message == "done"){                 x=true;             }             return x;         }          @override         protected void onpreexecute(){             super.onpreexecute();             pdialog = new progressdialog(home.this);             pdialog.setmessage("transaction in progress... \n have right remain \n silent & patient :)");             pdialog.setindeterminate(false);             pdialog.setcancelable(true);             pdialog.show();         }          @override         protected string doinbackground(string... args) {               list<namevaluepair> params = new arraylist<namevaluepair>();             params.add(new basicnamevaluepair("message", message));              jsonobject json = jsonparser.makehttprequest(url_addmessage, "get", params);              log.d("create response", json.tostring());              try{                 int success = json.getint(tag_success);                 if(success == 1){                     message = "done";                     log.d("work done", "message added");                 }                 else{                      log.d("work done", "message not added");                 }             } catch(jsonexception e){                 e.printstacktrace();              }              return null;         }          protected void onpostexecute(string file_url){             pdialog.dismiss();             if(new registerme().workdone(message)){                 toast.maketext(home.this, "message added successfully.",toast.length_long).show();             }             else{                 toast.maketext(home.this, "sorry there error in processing request, please try after time.",toast.length_long).show();             }           }     }  } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -