php - httpURLConnection.getInputStream() - 403 response - android -


i learning android. trying achieve here simple connection between server , android device.

i using wamp , have installed it. when try hitting php page (server script retieve db) via browser (using postman) able achieve want.

i configured own network ip url below , worked fine in wamp , did work in postman well. using real android device , in same connection of system (wifi)

my code

i have 2 activies , corresponding xml's

  1. mainactivity ( login page)
  2. register
  3. welcome (on successful login)

i have class named backgroundtask perform transaction

backgroundtask class

public class backgroundtask extends asynctask<string,void,string> {   context ctx;      backgroundtask(context ctx)     {         this.ctx = ctx;     }      @override     protected void onpreexecute() {         super.onpreexecute();     }      @override     protected string doinbackground(string... params) {         string reg_url = "http://<my ip here>/<myproject>/register.php";         string login_url = "http://<my ip here>/<myproject>/login.php";         string method = params [0];         if(method.equals("register"))         {             string firname = params[1];             string lasname = params[2];             string ages = params[3];             try {                 url url = new url(reg_url);                 httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection();                 httpurlconnection.setrequestmethod("post");                 httpurlconnection.setdooutput(true);                 outputstream os = httpurlconnection.getoutputstream();                 bufferedwriter bufferedwriter = new bufferedwriter(new outputstreamwriter(os,"utf-8"));                 string data = urlencoder.encode("firstname","utf-8")+ "="+urlencoder.encode(firname,"utf-8")+ "&" +                               urlencoder.encode("lastname","utf-8")+ "="+urlencoder.encode(lasname,"utf-8")+ "&" +                               urlencoder.encode("age","utf-8")+ "="+urlencoder.encode(ages,"utf-8");                 bufferedwriter.write(data);                 bufferedwriter.flush();                 bufferedwriter.close();                 os.close();           int responsecode = httpurlconnection.getresponsecode();                 if (responsecode >= 400 && responsecode <= 499) {                 throw new exception("bad authentication status: " + responsecode); //provide more meaningful exception message                  }                 else {                 //get response server                     inputstream = httpurlconnection.getinputstream();                     is.close();                  }                 return  "registration successful";             } catch (malformedurlexception e) {                 e.printstacktrace();             } catch (ioexception e) {                 e.printstacktrace();             } catch (exception e) {                 e.printstacktrace();             }          }         return null;     }      @override     protected void onprogressupdate(void... values) {         super.onprogressupdate(values);     }       @override     protected void onpostexecute(string result) {         toast.maketext(ctx,result,toast.length_short).show();     }  } 

register activity

public class register extends appcompatactivity {      edittext fname, lname, age;     button send;     string firstname,lastname,age;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_register);         initializevars();     }      public void initializevars() {         fname = (edittext) findviewbyid(r.id.etfirstname);         lname = (edittext) findviewbyid(r.id.etlastname);         age = (edittext) findviewbyid(r.id.etage);         send = (button) findviewbyid(r.id.btnsubmit);     }      public void userreg(view view)     {         firstname = fname.gettext().tostring();         lastname = lname.gettext().tostring();         age = age.gettext().tostring();          string method = "register";         backgroundtask backgroundtask = new backgroundtask(this);         backgroundtask.execute(method,firstname,lastname, age);         finish();      }  } 

error message

bad authentic status : 403 in part of code

 int responsecode = httpurlconnection.getresponsecode();                     if (responsecode >= 400 && responsecode <= 499) {                     throw new exception("bad authentication status: " + responsecode); //provide more meaningful exception message                      }                     else {                     //get response server                         inputstream = httpurlconnection.getinputstream();                         is.close();                      } 

what tried far

i looked @ few stackoverflow answers unable figure out issue. tried using

geterrorstream

i did not error message, data did not reach database.

please let me know if server scripts required additional info , add them well

requesting guidance , support.

thanks

edit

my php scripts

init.php

<?php  $db_name = "dbname"; $mysql_user = "root"; $mysql_pass = "<mypassword here>"; $server_name = "localhost";  $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);  if(!$con)      {         //echo "connection error".mysqli_connect_error();     }else     {         //echo "<h3> connection success  </h3>";     }  ?> 

register.php

<?php      require "init.php";      $first_name = $_post["firstname"];     $last_name = $_post["lastname"];     $age = $_post["age"];      $sql_query = "insert details values ('$first_name','$last_name','$age');";      if(mysqli_query($con,$sql_query))     {         //echo "<h3> data insertion successful </h3>";     } else         {             //echo "error".mysqli_error($con);         }      ?> 

screenshot of postman & db

postman image

db update

when replace localhost ip, works well. in db pic, u can see 2 records. bad. hit 'send' button twice in postman

edit 2 -

i tried using android emulator rather physical device. changed ip emulator's ip (10.0.x.x) worked charm. able see records in db well.

as suggested @cricket_007 feel ip issue. on front helpful. thanks


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -