I am using a json object request to get information from a database that I have created. I am able with my android application to insert data into the database but now I want to check for example if the user created an user account. I am getting a syntax error when I am trying to create the json object request. It is somehow a problem with the arguments but I do not understand what seems to be the problem. Both the url and the php files work fine.
String showUrl = "http://192.168.0.16/webapps/showUser.php";
public void searchLoginInfo(View view) {
JsonObjectRequest jsonObjectRequest = new /*HERE IS WHERE I GET THE ERROR*/ JsonObjectRequest(Request.Method.POST,showUrl,(String)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");
String password = user.getString("password");
if (username.equals(myLoginList.get(0)) && password.equals(myLoginList.get(1))) {
Toast.makeText(LoginActivity.this, "Login Succesfull", Toast.LENGTH_LONG).show();
Intent send = new Intent(LoginActivity.this, WelcomeActivity.class);
startActivity(send);
break;
}else{
Toast.makeText(LoginActivity.this, "Login Failed!", Toast.LENGTH_LONG).show();
Intent send = new Intent(LoginActivity.this, LoginActivity.class);
startActivity(send);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley Error", error.toString());
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null) {
Log.e("Status code", String.valueOf(networkResponse.statusCode));
}
}
});
requestQueue.add(jsonObjectRequest);
}
This is the error that I get: Error:(60, 98) error: incompatible types: String cannot be converted to JSONObject.
This is my showUser.php
<?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');
json_encode(array("users"=>$temp_array));
mysqli_close($connect);
}
?>
Just remove the cast to String
. Try this,
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl, null,
new Response.Listener<JSONObject>(){
@Override
public void onResponse(JSONObject response) {
}
}
);
The error says the server is returning a String
output. You can use a StringRequest
to resolve the error,
StringRequest stringRequest = new StringRequest(Request.Method.POST, showUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("response", response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);