java - Unable to do PHP+MySQL login with POST request -


i creating app , when click login in android simulator says unfortunately app has stopped checked code multiple times , don't see errors code . following tutorial should correct because works guy doing tutorial .

mainactivity.java :

import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.edittext;  public class mainactivity extends appcompatactivity {  edittext usernameet, passwordet;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     usernameet = (edittext)findviewbyid(r.id.etusername);     passwordet = (edittext)findviewbyid(r.id.etpassword);  }  public void onlogin(view view) {     string username = usernameet.gettext().tostring();     string password = passwordet.gettext().tostring();     string type = "login";     backgroundworker backgroundworker = new backgroundworker(this);     backgroundworker.execute(type, username, password);  }  } 

backgroundworker.java :

import android.app.alertdialog; import android.content.context; import android.os.asynctask;  import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstream; import java.io.outputstreamwriter; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.net.urlencoder;  public class backgroundworker extends asynctask<string,void,string> { context context; alertdialog alertdialog; backgroundworker (context ctx) {     context = ctx; }  @override protected string doinbackground(string... params) {     string type = params[0];     string login_url = "http://192.168.1.7/login.php";     if (type.equals("login")){         try {             string user_name = params[1];             string password = params[2];             url url = new url(login_url);             httpurlconnection httpurlconnection = (httpurlconnection)url.openconnection();             httpurlconnection.setrequestmethod("post");             httpurlconnection.setdooutput(true);             httpurlconnection.setdoinput(true);             outputstream outputstream = httpurlconnection.getoutputstream();             bufferedwriter bufferedwriter = new bufferedwriter(new outputstreamwriter(outputstream, "utf-8"));             string post_data = urlencoder.encode("user_name" , "utf-8")+"="+urlencoder.encode(user_name , "utf-8")+"&"                     +urlencoder.encode("user_pass" , "utf-8")+"="+urlencoder.encode(password , "utf-8");             bufferedwriter.write(post_data);             bufferedwriter.flush();             bufferedwriter.close();             outputstream.close();             inputstream inputstream = httpurlconnection.getinputstream();             bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(inputstream, "iso-8859-1"));             string result="";             string line;             while((line = bufferedreader.readline())!= null ){                 result += line;             }             bufferedreader.close();             inputstream.close();             httpurlconnection.disconnect();             return result;          } catch (malformedurlexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }      }     return null; }  @override protected void onpreexecute() {     alertdialog = new alertdialog.builder(context).create();     alertdialog.settitle("login status"); }  @override protected void onpostexecute(string result) {     alertdialog.setmessage(result);     alertdialog.show(); }  @override protected void onprogressupdate(void... values) {     super.onprogressupdate(values); } } 

login button in content_main.xml :

  <button     style="?android:attr/buttonstylesmall"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="login"     android:id="@+id/login"     android:layout_alignparentbottom="true"     android:layout_alignparentright="true"     android:layout_alignparentend="true"     android:onclick="onlogin"     />    <edittext     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:inputtype="textpersonname"     android:hint="username"     android:ems="10"     android:id="@+id/etusername"     android:layout_below="@+id/textview"     android:layout_alignparentleft="true"     android:layout_alignparentstart="true" />    <edittext     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:inputtype="textpassword"     android:ems="10"     android:hint="password"     android:id="@+id/etpassword"     android:layout_below="@+id/etusername"     android:layout_alignparentleft="true"     android:layout_alignparentstart="true" /> 

login.php :

<?php require"db.php";  $user_name = "user"; $password = "pass";  $mysql_qry = "select * users username = '$user_name' , password = '$password';";  $result = mysqli_query($conn, $mysql_qry);  if(mysqli_num_rows($result) > 0) { echo"login success"; } else { echo"not success"; } ?> 

error :

caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.text.editable android.widget.edittext.gettext()' on null object reference @ com.example.user.app.mainactivity.onlogin(mainactivity.java:22)

(line 22 mainactivity: string username = usernameet.gettext().tostring();)

once fix app crashing, such making sure activity_main.xml (or content_main, if have one) contains 2 button ids you've used in code, have second problem server well.

your php file uses user:pass credentials try login database, , never receives else.

if values android set in post request, can

if (isset($_post['user_name']) && isset($_post['user_pass'])) {      // receiving post params     $name = $_post['user_name'];     $password = $_post['user_pass'];      // todo: call database function  

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -