我正在进行移动应用程序的登录和注册子系统,我是通过关注youtube教程视频完成的,注册功能运行良好,因为所有数据都已成功存储在数据库中。
但是当我尝试使用已创建的用户名登录时,它会显示一条消息"用户名和密码不存在!",AS不会返回任何错误消息。我检查过所有的编码几次,谷歌搜索答案,但我找不到任何,提前谢谢! =)
**现在我确定我的活动没有任何问题,但是php脚本文件
的LocalDB
public class LocalDb {
public static final String SP_Name = "UserInfo";
SharedPreferences localDb;
//Constructor
public LocalDb(Context context)
{
localDb = context.getSharedPreferences(SP_Name,0);
}
//store user data
public void storeData(Contact contact)
{
SharedPreferences.Editor editor = localDb.edit();
editor.putString("Username",contact.username);
editor.putString("Email",contact.email);
editor.putString("Student_ID",contact.student_id);
editor.putString("Password",contact.password);
editor.commit();
}
//get information about which user is currently logged in
public Contact getLoggedInUser()
{
String username=localDb.getString("Username","");
String email=localDb.getString("Email","");
String student_id=localDb.getString("Student_ID","");
String password=localDb.getString("Password","");
Contact storedContact=new Contact(username,email,student_id,password);
return storedContact;
}
public void setUserLoggedIn(boolean loggedIn)
{
SharedPreferences.Editor editor=localDb.edit();
editor.putBoolean("loggedIn",loggedIn);
editor.commit();
}
//Tell user whether is currently login
public Boolean getUserLoggedIn()
{
if(localDb.getBoolean("loggedIn",false))
return true;
else
return false;
}
//Clear data when a user log out
public void clearData(){
SharedPreferences.Editor editor = localDb.edit();
editor.clear();
editor.commit();
}
}
以下是php文件:
Register.php
<?php
$con = mysqli_connect("localhost" , "my_db_name" , "my_password" , "my_db");
$username = $_POST["Username"];
$email = $_POST["Email"];
$student_id = $_POST["Student_ID"];
$password = $_POST["Password"];
$insertquery = mysqli_prepare($con , "INSERT INTO UserDetails (Username , Email , Student_ID , Password ) VALUES (? , ? , ? , ?)");
mysqli_stmt_bind_param($insertquery , "ssss" , $username , $email , $student_id , $password);
mysqli_stmt_execute($insertquery);
mysqli_stmt_close($insertquery);
mysqli_close($con);
?>
getUserData.php
<?php
$con = mysqli_connect("localhost" , "my_db_name" , "my_password" , "my_db");
$username = $_POST["Username"];
$password = $_POST["Password"];
$selectquery = mysqli_prepare($con , "SELECT * FROM UserDetails WHERE Username= ? AND Password= ?");
mysqli_stmt_bind_param($selectquery , "ss" , $username , $password);
mysqli_stmt_execute($selectquery);
mysqli_stmt_store_result($selectquery);
mysqli_stmt_bind_result($selectquery , $username , $email , $student_id , $password);
$user=array();
while(mysqli_stmt_fetch($selectquery);
(
$user[username] = $username;
$user[email] = $email;
$user[student_id] = $student_id;
Suser[password] = $password;
)
echo json_encode($user);
mysqli_stmt_close($selectquery);
mysqli_close($con);
?>
答案 0 :(得分:0)
我当然无法理解您的代码,但尝试使用新的Networking Lib。(“Volley”)进行通话。我登录和注册的例子。
//Layout of Activity
<?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:padding="2dp"
tools:context="com.test.test.ScreenOne">
<EditText
android:layout_width="240dp"
android:layout_height="wrap_content"
android:id="@+id/etUsername"
android:layout_marginTop="150dp"
android:hint="username"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="240dp"
android:layout_height="wrap_content"
android:id="@+id/etPassword"
android:hint="password"
android:layout_below="@+id/etUsername"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/bLogin"
android:layout_below="@+id/etPassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/bSave"
android:layout_below="@+id/bLogin"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
</RelativeLayout>
Activity有2个按钮和2个EditText,登录按钮通过服务器登录,Save按钮将数据保存在服务器中:
package com.test.test;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
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 java.util.HashMap;
import java.util.Map;
public class ScreenOne extends AppCompatActivity {
private static final String URL_LOGIN = "http://IP_ADDRESS/login.php";
private static final String URL_SAVE = "http://IP_ADDRESS/save.php";
private EditText username;
private EditText password;
private Button login;
Button save;
String name;
String pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_one);
username = (EditText) findViewById(R.id.etUsername);
password = (EditText) findViewById(R.id.etPassword);
(login = (Button) findViewById(R.id.bLogin)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
request();
}
});
(save = (Button) findViewById(R.id.bSave)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveRequest();
}
});
}
private void saveRequest() {
name = username.getText().toString().trim();
pass = password.getText().toString().trim();
final ProgressDialog mDialog = new ProgressDialog(this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setMessage("Loading...");
mDialog.show();
StringRequest request = new StringRequest(Request.Method.POST, URL_SAVE,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mDialog.dismiss();
Toast.makeText(ScreenOne.this, response, Toast.LENGTH_LONG).show();
username.setText("");
password.setText("");
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mDialog.dismiss();
Toast.makeText(ScreenOne.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> key = new HashMap<>();
key.put("username", name);
key.put("password", pass);
return key;
}
};
NetworkCalls.getInstance().addToRequestQueue(request);
}
private synchronized void request() {
name = username.getText().toString().trim();
pass = password.getText().toString().trim();
final ProgressDialog mDialog = new ProgressDialog(this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setMessage("Loading...");
mDialog.show();
StringRequest request = new StringRequest(Request.Method.POST, URL_LOGIN,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mDialog.dismiss();
Toast.makeText(ScreenOne.this, response, Toast.LENGTH_LONG).show();
username.setText("");
password.setText("");
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mDialog.dismiss();
Toast.makeText(ScreenOne.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> key = new HashMap<>();
key.put("username", name);
key.put("password", pass);
return key;
}
};
NetworkCalls.getInstance().addToRequestQueue(request);
}
}
Volley请求的Singleton类:
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
/**
* Created by W4R10CK on 12-09-2016.
*/
public class NetworkCalls {
private RequestQueue requestQueue;
private static Context context;
private static NetworkCalls ourInstance = new NetworkCalls();
public static NetworkCalls getInstance() {
return ourInstance;
}
private NetworkCalls() {
}
public RequestQueue getRequestQueue(){
requestQueue = Volley.newRequestQueue(context.getApplicationContext());
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> request){
getRequestQueue().add(request);
}
}
调用服务器的API:
//conn.php for connection (file one)
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db_name = "hello";
$con = new mysqli($host,$user,$pass,$db_name);
if($con -> connect_error){
echo "Connection error";
}
//save.php(file two)
<?php
$username = $_POST['username'];
$password = $_POST['password'];
require_once('conn.php');
$sql = "INSERT INTO user (username, password) VALUES ('$username','$password')";
if($con -> query($sql) === TRUE) {
echo "User added";
}
//$con -> close();
?>
?>
//login.php(file three)
<?php
require_once('conn.php');
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM user WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($con,$sql);
if(mysqli_fetch_array($result) == NULL){
echo "Invalid Cred.";
}else{
echo "Success";
}
$con->close();
?>
最后在localhost 用户中创建一个名为 hello 的数据库,其中包含2个字段用户名,密码。< / p>