0

I am developing an app for android and now I am doing the user "CreateAccount" and "Login" activities. I am able to insert the user's info in the database, but I am not able to get the user's info from the database to perform the login. This is my java code:

String showUrl = "http://192.168.0.11/webapps/showUser.php";
public void searchLoginInfo(View view) {
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,showUrl,null, new Response.Listener<JSONObject>() 
        {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray users = response.getJSONArray("users");
                for (int i = 0; i < users.length(); i++) {
                    JSONObject user = users.getJSONObject(i);
                    String username = user.getString("username").toLowerCase();
                    String password = user.getString("password").toLowerCase();
                    String retypedPassword = user.getString("retypedpassword").toLowerCase();
                    String email = user.getString("passphrase").toLowerCase();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() 
       {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "onErrorResponse: ERORR!!!!!!!!!!!" );
            Log.e(TAG, "onErrorResponse: ERORR!!!!!!!!!!!"+error.toString() );
        }
    });
    requestQueue.add(jsonObjectRequest);
}

The error that I get is: E/MyActivity: onErrorResponse: ERORR!!!!!!!!!!! E/MyActivity: onErrorResponse: ERORR!!!!!!!!!!!com.android.volley.ParseError: org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONObject. This is my showUser.php script :

<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
include 'connection.php';
showUser();
}
function showUser(){
global $connect;
$query="SELECT* FROM user;";
$result=mysqli_query($connect,$query);
$number_of_rows=mysqli_num_rows($result);
$temp_array=array();
if($number_of_rows>0){
    while($row=mysqli_fetch_assoc($result)){
        $temp_array[]=$row;
    }
}
header('Content-Type:application/json');
echo json_encode(array("users"=>$temp_array));
mysqli_close($connect);
}
?>

From what I have red on the internet there could be a problem with the collation?! My table collation and columns is utfmb4_general_ci and my server connection collation is utfmb4_unicode_ci PrintScreen

I have also used POSTMAN to see the actual response from the server and it looks OK to me.

{
  "users": [
{
  "username": "charlie",
  "password": "harris",
  "retypedpassword": "harris",
  "email": "charlie@yahoo.com"
},
{
  "username": "tomas",
  "password": "kovtun",
  "retypedpassword": "kovtun",
  "email": "tkovtu@yahoo.com"
}
]
}

This problem is really killing me guys so any help will be appreciated! Thx!

4

1 回答 1

0

用这个:

public void searchLoginInfo(View view) {
StringRequest stringRequest = new StringRequest(Request.Method.POST,showUrl, new Response.Listener<String>() 
    {
    @Override
    public void onResponse(String response) {
        try {
            response  = response.replaceAll("[^\\p{ASCII}]", "");
            JSONObject jsonObject = new JSONObject(response);
            JSONArray users = jsonObject.getJSONArray("users");
            for (int i = 0; i < users.length(); i++) {
                JSONObject user = users.getJSONObject(i);
                String username = user.getString("username").toLowerCase();
                String password = user.getString("password").toLowerCase();
                String retypedPassword = user.getString("retypedpassword").toLowerCase();
                String email = user.getString("passphrase").toLowerCase();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}, new Response.ErrorListener() 
   {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(TAG, "onErrorResponse: ERORR!!!!!!!!!!!" );
        Log.e(TAG, "onErrorResponse: ERORR!!!!!!!!!!!"+error.toString() );
    }
});
requestQueue.add(stringRequest);
}
于 2016-11-12T17:16:36.740 回答