please guys i need ur help!.. I am new to Volley and i have been trying to do the login and registeration using volley. To say Insertion and retrieval. The problem is i cannot retrieve o insert anything i the db im using json array and json object. Please help me. My files are here below.
MainActivity
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
Button login_button;
EditText Username,Password;
String username,password;
String login_url = "http://IP_ADDRS/VolleyAndroid/login.php";
AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
builder = new AlertDialog.Builder(MainActivity.this);
login_button = (Button)findViewById(R.id.login_btn);
Username = (EditText)findViewById(R.id.emailln);
Password = (EditText)findViewById(R.id.passln);
login_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
username = Username.getText().toString();
password = Password.getText().toString();
if(username.equals("") || password.equals("")){
builder.setTitle("Something Went wrong");
displayAlert("Enter a valid username and password");
}
else{
StringRequest stringRequest = new StringRequest(Request.Method.POST, login_url,
new Response.Listener<String>(){
@Override
public void onResponse(String response){
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String code = jsonObject.getString("code");
if(code.equals("login_failed")){
builder.setTitle("Login Error");
displayAlert(jsonObject.getString("message"));
}
else{
Intent intent = new Intent (MainActivity.this,LoginSuccess.class);
Bundle bundle = new Bundle();
bundle.putString("name",jsonObject.getString("name"));
bundle.putString("email",jsonObject.getString("email"));
intent.putExtras(bundle);
startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("user_name",username);
params.put("password",password);
return params;
}
};
MySingleton.getInstance(MainActivity.this).addToRequest(stringRequest);
}
}
});
}
public void displayAlert(String message){
builder.setMessage(message);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Username.setText("");
Password.setText("");
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
public void register(View v){
Intent intent = new Intent(getApplicationContext(),Register.class);
startActivity(intent);
}
}
Register
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class Register extends AppCompatActivity {
Button reg_bn;
EditText Name,Phone,Email,Password,ConPassword;
String name,phone,email,password,conpass;
AlertDialog.Builder builder;
String reg_url = "http://IP_ADDRS/VolleyAndroid/register.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
reg_bn = (Button)findViewById(R.id.registerbtn);
Name = (EditText)findViewById(R.id.nameedit);
Phone = (EditText)findViewById(R.id.phoneedit);
Email = (EditText)findViewById(R.id.emailedit);
Password = (EditText)findViewById(R.id.passedit);
ConPassword = (EditText)findViewById(R.id.cpassedit);
builder = new AlertDialog.Builder(Register.this);
reg_bn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
name = Name.getText().toString();
phone = Phone.getText().toString();
email = Email.getText().toString();
password = Password.getText().toString();
conpass = ConPassword.getText().toString();
if(name.equals("") || phone.equals("") || email.equals("") || password.equals("") || conpass.equals("")){
builder.setTitle("Something went Wrng!!");
builder.setMessage("Please enter all the feilds");
displayAlert("input_err");
}
else{
if(!(password.equals(conpass))){
builder.setTitle("Something went wrong!!");
builder.setMessage("Password incorrect!");
displayAlert("input_err");
}
else{
StringRequest stringRequest = new StringRequest(Request.Method.POST, reg_url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String code = jsonObject.getString("code");
String message = jsonObject.getString("message");
builder.setTitle("Response from server");
builder.setMessage(message);
displayAlert(code);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
public void onErrorResponse(VolleyError error){
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError{
Map<String, String> params =new HashMap<String, String>();
params.put("name",name);
params.put("phone",phone);
params.put("email",email);
params.put("pass",password);
return params;
}
};
MySingleton.getInstance(Register.this).addToRequest(stringRequest);
}
}
}
});
}
public void displayAlert(final String code){
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(code.equals("input_error"))
{
Password.setText("");
ConPassword.setText("");
}
else if(code.equals("reg_success"))
{
finish();
}
else if(code.equals("reg_failed")){
Name.setText("");
Phone.setText("");
Email.setText("");
Password.setText("");
ConPassword.setText("");
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
MySingleton
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
/**
* Created by sshank on 5/1/17.
*/
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue requestQueue;
private static Context mctx;
private MySingleton(Context context){
mctx = context;
requestQueue = getRequestQueue();
}
private RequestQueue getRequestQueue() {
if(requestQueue == null)
{
requestQueue = Volley.newRequestQueue(mctx.getApplicationContext());
}
return requestQueue;
}
public static synchronized MySingleton getInstance(Context context)
{
if(mInstance == null)
{
mInstance = new MySingleton(context);
}
return mInstance;
}
public <T>void addToRequest(Request<T> request)
{
requestQueue.add(request);
}
}
My layout files
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.company.sshank.volleyimplementation.MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Easy Travel Connect"
android:textAlignment="center"
android:textSize="25dp"
android:layout_marginTop="35dp"
/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/block1"
android:layout_below="@+id/title"
android:layout_marginTop="50dp">
<TextView
android:id="@+id/emailtxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email"></TextView>
<EditText
android:id="@+id/emailln"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/emailtxt"></EditText>
<TextView
android:id="@+id/passtxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Password"
android:layout_below="@+id/emailln"></TextView>
<EditText
android:id="@+id/passln"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/passtxt"></EditText>
<Button
android:id="@+id/login_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/passln"
android:layout_marginTop="25dp"
android:text="Sign In"></Button>
</RelativeLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/block2"
android:layout_below="@+id/block1">
<TextView android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/registerlink"
android:text="Not a user? Connect now!"
android:onClick="register"
android:gravity="center"
android:layout_marginTop="10dp"/>
<TextView android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/terms"
android:layout_below="@+id/registerlink"
android:text="I Agree to the terms and conditions"
android:layout_marginTop="120dp"
android:textStyle="bold"
android:gravity="center" />
</RelativeLayout>
</RelativeLayout>
activity_register
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_register"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.company.sshank.volleyimplementation.Register">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Easy Travel Connect"
android:textAlignment="center"
android:textSize="25dp"
android:layout_marginTop="25dp"
/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/block1"
android:layout_below="@+id/title"
android:layout_marginTop="30dp">
<TextView
android:id="@+id/nametxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"></TextView>
<EditText
android:id="@+id/nameedit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/nametxt"></EditText>
<TextView
android:id="@+id/phonetxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Phone"
android:layout_below="@+id/nameedit"></TextView>
<EditText
android:id="@+id/phoneedit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/phonetxt"></EditText>
<TextView
android:id="@+id/emailtxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email"
android:layout_below="@+id/phoneedit"></TextView>
<EditText
android:id="@+id/emailedit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/emailtxt"></EditText>
<TextView
android:id="@+id/passtxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Password"
android:layout_below="@+id/emailedit"></TextView>
<EditText
android:id="@+id/passedit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/passtxt"></EditText>
<TextView
android:id="@+id/cpasstxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Confirm Password"
android:layout_below="@+id/passedit"></TextView>
<EditText
android:id="@+id/cpassedit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/cpasstxt"></EditText>
<Button
android:id="@+id/registerbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/cpassedit"
android:layout_marginTop="15dp"
android:text="Register"></Button>
</RelativeLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/block2"
android:layout_below="@+id/block1">
<TextView android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/terms"
android:layout_below="@+id/registerlink"
android:text="I Agree to the terms and conditions"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:gravity="center" />
</RelativeLayout>
</RelativeLayout>
Php files.. here im retrieving and inserting data
login
<?php
$host = "localhost";
$user = "username";
$pass = "";
$db = "db_name";
$conn = mysqli_connect($host, $user, $pass, $db);
$email = $_POST['email'];
$password = $_POST['password'];
$query = "select * from Login where email like '".$email."'and password like '".$password."';";
$result = mysqli_query($conn, $query);
$response = array();
if(mysqli_num_rows($result)>0)
{
$row = mysqli_fetch_row($result);
$name = $row[0];
$phone = $row[1];
$code = "login_success";
array_push($response, array("code"=>$code,"name"=>$name,"phone"=>$phone));
echo json_encode($response);
}else{
$code = "login_failed";
$message = "User not found...Please try again...";
array_push($response, array("code"=>$code,"message"=>$message));
echo json_encode($response);
}
mysqli_close($conn);
?>
Register
<?php
$host = "localhost";
$user = "username";
$pass = "";
$db = "dbname";
$conn = mysqli_connect($host, $user, $pass, $db);
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$password = $_POST['password'];
$query = "select * from Login where email like '".$email."';";
$result = mysqli_query($conn, $query);
$response = array();
if(mysqli_num_rows($result)>0)
{
$code = "reg_failed";
$message = "User exist";
array_push($response, array("code"=>$code,"message"=>$message));
echo json_encode($response);
}else{
$query = "insert into Login (name,phone,email,password)
values ('".$name."', '".$phone."', '".$email."', '".$password."')";
$result = mysqli_query($conn, $query);
$code = "reg_success";
$message = "Thank you for rgistering with us!! Now you can login!";
array_push($response, array("code"=>$code,"message"=>$message));
echo json_encode($response);
}
?>