android - How to connect local server to GCM client application? -


i have built gcm application , local server in php. want store registration id of client server database. running server on localhost. tried use ipv4 address 192.168.x.y/gcm/save.php(it's running fine , it's identifying files in c:/wamp/www when run on computer) , connected both local computer , android application same wifi network isn't working. how can send regid local server running on computer? here utils.java file:

public class utils {  static context context; public static final string tag = "utils"; public static final string username = "username";  public static final string property_reg_id = "registration_id"; private static final string property_app_version = "appversion";  public utils(context context) {     utils.context = context; }  public sharedpreferences getgcmpreferences() {     return context.getsharedpreferences(((actionbaractivity) context)             .getclass().getsimplename(), context.mode_private); }  public void savepreferences(string key, string value) {     final sharedpreferences prefs = getgcmpreferences();     log.i(tag, key + " : " + value);     sharedpreferences.editor editor = prefs.edit();     editor.putstring(key, value);     editor.commit(); }  public string getfrompreferences(string key) {     final sharedpreferences prefs = getgcmpreferences();     string value = prefs.getstring(key, "");     if (value.isempty()) {         log.i(tag, key + " not found.");         return "";     }     return value; }  string getregistrationid() {     final sharedpreferences prefs = getgcmpreferences();     string registrationid = prefs.getstring(property_reg_id, "");     if (registrationid.isempty()) {         log.i(tag, "registration not found.");         return "";     }     // check if app updated; if so, must clear registration id     // since existing regid not guaranteed work new     // app version.     int registeredversion = prefs.getint(property_app_version,             integer.min_value);     int currentversion = getappversion();     if (registeredversion != currentversion) {         log.i(tag, "app version changed.");         return "";     }     return registrationid; }  static int getappversion() {     try {         packageinfo packageinfo = context.getpackagemanager()                 .getpackageinfo(context.getpackagename(), 0);         return packageinfo.versioncode;     } catch (namenotfoundexception e) {         // should never happen         throw new runtimeexception("could not package name: " + e);     } }  public void storeregistrationid(string regid) {     final sharedpreferences prefs = getgcmpreferences();     int appversion = utils.getappversion();     log.i(tag, "saving regid on app version " + appversion);     log.i(tag, "reg id : " + regid);     sharedpreferences.editor editor = prefs.edit();     editor.putstring(property_reg_id, regid);     editor.putint(property_app_version, appversion);     editor.commit(); }  public string getcurrentipaddress() {     //return "http://192.168.1.37/safety_app/save_reg_id.php";     return "http://192.168.14.198/safety_app/save_reg_id.php"; }  public void showtoast(final string txt) {     ((activity) context).runonuithread(new runnable() {         public void run() {             toast.maketext(context, txt, toast.length_long).show();         }     });  }  } 

and here sendregistrationidtobackend function:

public void sendregistrationidtobackend() {      log.i(tag, "sendregistrationidtobackend");      thread thread = new thread() {          @override         public void run() {             try {                 httpclient = new defaulthttpclient();                  httppost = new httppost(utils.getcurrentipaddress());                  namevaluepairs = new arraylist<namevaluepair>(1);                  namevaluepairs.add(new basicnamevaluepair("username",                         user_name));                  namevaluepairs.add(new basicnamevaluepair("reg_id", regid));                  httppost.setentity(new urlencodedformentity(namevaluepairs));                  responsehandler<string> responsehandler = new basicresponsehandler();                 utils.showtoast("reached 6");                     httpresponse response = httpclient.execute(httppost);                 string responsebody = entityutils.tostring(response.getentity());                 utils.showtoast("reached 7");                 log.i(tag, "response : " + response);                 utils.showtoast("reached here");                  if (responsebody != null) {                     utils.showtoast("reached nn");                     if (responsebody.equalsignorecase("username registered")) {                          utils.showtoast("username registered");                          hidepb();                      } else {                         if (responsebody                                 .equalsignorecase("new device registered successfully")) {                              utils.savepreferences(utils.username, user_name);                             // persist regid - no need register                             // again.                             utils.storeregistrationid(regid);                              utils.showtoast("device registration successful");                              runonuithread(new runnable() {                                 public void run() {                                      setpeoplelist();                                 }                             });                          }                     }                  }              } catch (exception e) {                 utils.showtoast("catch-"+e.getmessage());                 //showpb("catch-"+e.getmessage());                 log.d(tag, "exception : " + e.getmessage());             }         }     };      thread.start();      } 

here save_reg_id.php:

$servername = "localhost"; $username = "root"; $password = ""; $dbname = "safety_app";  // create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // check connection if (!$conn) { die("connection failed: " . mysqli_connect_error()); }  $name = $_post["username"]; // = "vvv"; $reg_id = $_post["reg_id"]; // =         "apa91beffaxquuolftf0jy55c5lshdl5ugu5nrsk_bmwd8wpyw6ht4dkw5geq2wfumfrqd2lovrf7kn7ctjrh-ctrwxxtelsfusi4w8l56bi35vywvylrwza0qz02qv9gfyshqenn6xv-s3yw5sloaxl-hka5zzfuizbnpicvqqseg20ckh0e10";  $duplicate_check_sql = "select * registereddevices username =      '$name'"; $suplicate_result = $conn->query($duplicate_check_sql);  if ($suplicate_result->num_rows > 0) {     echo "username registered";  }else{  $sql = "insert registereddevices (username, reg_id) values ('$name', '$reg_id')";  if (mysqli_query($conn, $sql)) {     echo "new device registered successfully"; } else {     echo "error: " . $sql . "<br>" . mysqli_error($conn); }  }         mysqli_close($conn);  ?> 


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -