nullpointerexception - Android Connection with MYSQL database using php -


i trying connect android login page mysql database present on server.

but facing problem in passing php file through android. connection php mysql database successful, though.

php code:

<?php     $host='localhost';     $uname='amodbina0106';     $pwd='amodbina200';     $db="kezin_king";      $con=mysqli_connect("localhost","amodbina0106","amodbina200","kezin_king");     if ($con->connect_error) {         die("connection failed: " . $con->connect_error);     }      echo "connection successful";       $username = $_get['username'];     $password = $_get['password'];     $flag['code']=0;      if($name == '' || $username == '' || $password == '' || $email == ''){         echo 'please fill values';     }     else{            $sql=mysql_query("insert sample values('$id','$name') ",$con);          if(mysqli_query($con,$sql))          {             $flag['code']=1;             echo"hi";         }          print(json_encode($flag));         mysql_close($con);     } ?> 

the output after running php file on web browser is:

connection successful please fill values

my mainactivity code:

package com.example.nupur.phpdb_con;  import android.content.context; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast;  import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicnamevaluepair; import org.json.jsonobject;  import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; import java.nio.buffer; import java.util.arraylist; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader;    public class mainactivity extends appcompatactivity {     edittext etusername, etpassword;     textview txtview;     button btnlogin;     string username;     string password;     inputstream = null;     string result = "";     string line = null;     int code;     inputstream inputstream = null;     bufferedreader reader;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         etusername = (edittext) findviewbyid(r.id.etusername);         etpassword = (edittext) findviewbyid(r.id.etpassword);         btnlogin = (button) findviewbyid(r.id.btnlogin);         txtview = (textview) findviewbyid(r.id.txtview);         btnlogin.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 username = etusername.gettext().tostring();                 password = etpassword.gettext().tostring();                 insert();                   // txtview.settext(etusername.gettext()+" "+etpassword.gettext());               }         });       }      public string insert() {         bufferedreader bufferedreader;          arraylist<namevaluepair> namevaluepairs = new arraylist<>();         namevaluepairs.add(new basicnamevaluepair("username", username));         namevaluepairs.add(new basicnamevaluepair("password", password));         try {              // create httpclient             httpclient httpclient = new defaulthttpclient();              // make request given url             httpresponse httpresponse = httpclient.execute(new         httpget("http://kezinking.com/sampleconlogin"));              // receive response inputstream             inputstream = httpresponse.getentity().getcontent();              // convert inputstream string             if (inputstream != null) {                 bufferedreader = new bufferedreader(new inputstreamreader(inputstream));                 while ((line = bufferedreader.readline()) != null)                     result += line;                  inputstream.close();                 return result;             } else                 result = "did not work!";          } catch (exception e) {             log.e("fail 1", e.tostring());             toast.maketext(getapplicationcontext(), "invalid ip address",                     toast.length_long).show();         }          try {              bufferedreader reader = new bufferedreader                     (new inputstreamreader(is, "utf-8"), 8);             stringbuilder sb = new stringbuilder();             while ((line = reader.readline()) != null) {                 sb.append(line + "\n");             }             is.close();             result = sb.tostring();             log.e("pass 2", "connection success ");         } catch (exception e) {             log.e("fail 2", e.tostring());         }         try {             jsonobject json_data = new jsonobject(result);             code = (json_data.getint("code"));             if (code == 1) {                 toast.maketext(getbasecontext(), "inserted successfully",                         toast.length_short).show();             } else {                 toast.maketext(getbasecontext(), "sorry, try again",                         toast.length_long).show();             }         } catch (exception e) {             log.e("fail 3", e.tostring());          }          return null;     }     // @override     //public boolean oncreateoptionsmenu(menu menu) {       //  getmenuinflater().inflate(r.menu.activity_main, menu);         //return true;     //}  } 

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/activity_main"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.example.nupur.phpdb_con.mainactivity">     <linearlayout         android:layout_width="wrap_content"         android:layout_height="wrap_content">        <edittext            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:ems="10"            android:id="@+id/etusername"            android:layout_marginbottom="10px"             android:hint="username"/>     </linearlayout>     <linearlayout         android:layout_width="wrap_content"         android:layout_height="wrap_content">     <edittext         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/etpassword"         android:ems="10"         android:layout_margintop="100px"         android:hint="password" />     </linearlayout>     <linearlayout         android:layout_width="wrap_content"         android:layout_height="wrap_content">     <button         android:id="@+id/btnlogin"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginleft="250px"         android:gravity="center"         android:text="@string/login"         android:layout_margintop="400px"/>     </linearlayout>     <textview         android:gravity="bottom"         android:id="@+id/txtview"         android:layout_width="wrap_content"         android:layout_height="wrap_content" />  </android.support.constraint.constraintlayout> 

androidmanifest.xml:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.nupur.phpdb_con">     <uses-permission android:name="android.permission.internet"/>     <uses-permission android:name="android.permission.access_network_state" />      <application         android:allowbackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:supportsrtl="true"         android:theme="@style/apptheme">         <activity android:name=".mainactivity">             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>     </application>  </manifest> 

after debugging error:

e/fail 1: android.os.networkonmainthreadexception e/fail 2: java.lang.nullpointerexception: lock == null e/fail 3: org.json.jsonexception: end of input @ character 0 of  

error in emulator:

invalid ip address

you cant echo string in php file , introduce jsonobject in android.

all error , message must in json array , json array showed in php file .

i suggest :

   <?php         $host='localhost';         $uname='amodbina0106';         $pwd='amodbina200';         $db="kezin_king";         $result = array();         $con=mysqli_connect("localhost","amodbina0106","amodbina200","kezin_king");     if ($con->connect_error) {    $result['status'] = false;    $result['message'] = 'server lost';         die(json_encode($result));     }                $username = $_get['username'];             $password = $_get['password'];             $flag['code']=0;              if($name == '' || $username == '' || $password == '' || $email == ''){                 $result['status'] = false;                   $result['message'] = 'all field required';             }             else{            $sql=mysql_query("insert sample values('$id','$name') ",$con);          if(mysqli_query($con,$sql))          {             $flag['code']=1;               $result['status'] = true;                   $result['message'] = 'hi';         }          print(json_encode($result));         mysql_close($con);         }     ?> 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -