I'm making an app where my registration works fine only that my login is not working my php script is working fine only in login it is saying json mismatch
here is my loginactivity
Just in login it is giving error apart from that everything is working fine
i checked php json response it is same as i have given in android still not working i don't know where i'm going wrong
sample json response
[{"code":"login_success","username":"smith","password":"55fd64d3d40f601a78f7917de5a10e6044a5e74d"}]
Error Response
[{"code":"login_failed","message":"User not found... Please try again"}]
Please find my Login Page:
public void login(View view) {
mProgress.show();
username = editusername.getEditText().getText().toString();
password = editpasword.getEditText().getText().toString();
Prefs prefs = new Prefs(LoginActivity.this);
prefs.setName(username);
if(username.equals("") || password.equals("")){
displayAlert("Enter a valid username and password");
}
else{
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response); //on this line it is giving error
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(LoginActivity.this,Work.class);
startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
displayAlert("Internet Connection problem");
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("username",username);
params.put("password",password);
return params;
}
};
Singleton.getInstance(LoginActivity.this).addToREquestQueue(stringRequest);
}
}
public void displayAlert(String message){
builder.setMessage(message);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
editpasword.getEditText().setText("");
editusername.getEditText().setText("");
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
SharedPreference.java
public class Prefs {
Context context;
private String name;
SharedPreferences sharedPreferences;
public void removeUser(){
sharedPreferences.edit().clear().commit();
}
public String getName() {
name = sharedPreferences.getString("userdata","");
return name;
}
public void setName(String name) {
this.name = name;
sharedPreferences.edit().putString("userdata",name).commit();
}
public Prefs(Context context){
this.context = context;
sharedPreferences = context.getSharedPreferences("userinfo",Context.MODE_PRIVATE);
}
}
Please see my Logcat detail:-
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 04-03 11:12:47.588 14388-14388/kashyapinfotech.com.officework W/System.err: at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5312) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 04-03 11:12:48.097 14388-14388/kashyapinfotech.com.officework W/System.err: org.json.JSONException: Value (JSONArray.java:96) at org.json.JSONArray.(JSONArray.java:108) at kashyapinfotech.com.officework.LoginActivity$1.onResponse(LoginActivity.java:68) at kashyapinfotech.com.officework.LoginActivity$1.onResponse(LoginActivity.java:64) at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) 04-03 11:12:48.098 14388-14388/kashyapinfotech.com.officework W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5312) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
I checked php it is working fine so here is php script
<?php
include("conn.php");
if(isset($_POST['btn_login'])){
$username = $_POST['username'];
$password = $_POST['password'];
$password = sha1($password);
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn,$sql);
$response = array();
if(mysqli_num_rows($result)>0){
$row = mysqli_fetch_row($result);
$username = $row[1];
$password = $row[3];
$code = "login_success";
array_push($response,array("code"=>$code,"username"=>$username,"password"=>$password));
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);
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post">
<input type="text" name="username">
<input type="text" name="password">
<input type="submit" name="btn_login">
</form>
</body>
</html>
You are checking for a key btn_login in your php code which you are not posting from your android end, for example it should be
params.put("username",username);
params.put("password",password);
params.put("btn_login","some data");
or if you dont need btn_login, remove it from php code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With