I am trying to make an android login app,where the login credentials of different users are in database.
<?php
require_once('conn2.php');
$username=$_GET['username'];
$password=$_GET['pass'];
$sql = "SELECT * FROM user_login WHERE Id='$username' and Password='$password'";
$res = mysqli_query($con,$sql);
$check = mysqli_fetch_row($res);
if(isset($check)){
echo 'success';
}
else{
echo $username,$password;
}
mysqli_close($con);
?>
The conn2.php is used to connect to the database.(No problem in that)
package com.example.chili_32.apollotyres;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
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.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.chili_32.apollotyres.Connection.Config;
import com.example.chili_32.apollotyres.Connection.RequestHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Login_page extends AppCompatActivity {
private static EditText Username;
private static EditText Password;
private static Button Sign_In;
private static FrameLayout Apollo;
TextView forgot;
Context context;
String JSON_STRING,pass,username;
int counter = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
Loginbutton();
}
public void Loginbutton(){
forgot = (TextView)findViewById(R.id.fp);
Username = (EditText)findViewById(R.id.editText_Name);
Password = (EditText)findViewById(R.id.editText_password);
Sign_In = (Button)findViewById(R.id.signin);
Apollo = (FrameLayout)findViewById(R.id.apollo);
forgot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Login_page.this,".....",Toast.LENGTH_SHORT).show();
}
});
Apollo.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent zed = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.apollotyres.com"));
startActivity(zed);
}
}
);
Sign_In.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
getJSON();
}
}
);}
private void getJSON() {
username=Username.getText().toString().trim();
pass=Password.getText().toString().trim();
Toast.makeText(Login_page.this, username+pass, Toast.LENGTH_SHORT).show();
class GetJSON extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
JSON_STRING = s;
if (JSON_STRING=="success") {
Toast.makeText(Login_page.this, "Logging In",
Toast.LENGTH_SHORT).show();
Intent abc = new Intent("com.example.chili_32.apollotyres.Home");
startActivity(abc);
}
else {
Toast.makeText(Login_page.this, "Incorrect Username or Password"+JSON_STRING, Toast.LENGTH_LONG).show();
counter--;
if (counter == 0) {
Sign_In.setEnabled(false);
}
}
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler(context);
return rh.sendGetRequestParam3(Config.URL_LOGIN,username,pass);
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
private void login(){
JSONObject jsonObject;
try{
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
for(int i = 0;i<result.length();i++) {
JSONObject jo = result.getJSONObject(i);
String password = jo.getString("Password");
Toast.makeText(Login_page.this, password, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
This is the code for RequestHandler Class
package com.example.chili_32.apollotyres.Connection;
import android.content.Context;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class RequestHandler {
Context context;
public RequestHandler(Context context) {
this.context = context;
}
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "iso-8859-1"));
String response;
while ((response = br.readLine()) != null) {
sb.append(response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
public String sendGetRequest(String requestURL) {
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(requestURL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String s;
while ((s = bufferedReader.readLine()) != null) {
sb.append(s + "
");
}
} catch (Exception e) {
}
return sb.toString();
}
public String sendGetRequestParam(String requestURL, String id){
StringBuilder sb =new StringBuilder();
try {
URL url = new URL(requestURL+id);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String s;
while((s=bufferedReader.readLine())!=null){
sb.append(s+"
");
}
}catch(Exception e){
}
return sb.toString();
}
public String sendGetRequestParam2(String requestURL, String id,int size){
StringBuilder sb =new StringBuilder();
try {
URL url = new URL(requestURL+id+size);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String s;
while((s=bufferedReader.readLine())!=null){
sb.append(s+"
");
}
}catch(Exception e){
}
return sb.toString();
}
public String sendGetRequestParam3(String requestURL, String id,String pass){
StringBuilder sb =new StringBuilder();
try {
URL url = new URL(requestURL+id+pass);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String s;
while((s=bufferedReader.readLine())!=null){
sb.append(s+"
");
}
}catch(Exception e){
}
return sb.toString();
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
}
The Config.java class has all the URLS and final static variables.Config.URL_Login is the url of login page.I declared some final static variables in it as I will be using very often in the code. Eg: Config.TAG_JSON_ARRAY = "result";
This is the xml file
<?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: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.example.chili_32.apollotyres.Login_page"
android:background="@drawable/nat">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Username :"
android:id="@+id/textView_Username"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="71dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Password :"
android:id="@+id/textView_Password"
android:layout_below="@+id/textView_Username"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="false"
android:paddingTop="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText_Name"
android:layout_above="@+id/editText_password"
android:layout_toRightOf="@+id/signin"
android:layout_toEndOf="@+id/signin" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText_password"
android:layout_alignTop="@+id/textView_Password"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="@+id/editText_Name"
android:layout_alignStart="@+id/editText_Name" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SIGN IN"
android:id="@+id/signin"
android:layout_centerVertical="true"
android:layout_marginLeft="100dp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="@+id/signin"
android:layout_marginTop="100dp"
android:id="@+id/apollo"></FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Forgot Password?"
android:id="@+id/fp"
android:layout_below="@+id/editText_password"
android:layout_alignLeft="@+id/editText_password"
android:layout_alignStart="@+id/editText_password"
android:textColorHighlight="#5555"/>
</RelativeLayout>
Well,when I use the URL http://localhost/login.php?username=2013A8PS454H&pass=anvhesh It shows success.But in my application ,as I mentioned in the code,it is displaying username and password. I'm struck here it would be great if anyone helps.