Tirth Allspark Rami Tirth Allspark Rami - 26 days ago 14
Java Question

org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONObject

I am writing a login function for my application and I suddenly started getting this error:

org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONObject
I don't recall modifying the code here at all and it used to work before. It suddenly stopped working when I started using the debug mode on android studio.

Here is a snippet of my code, the error seems to be on the 2 lines marked with /*ERROR*/:

private String loginUrl = "http://10.0.2.2/user/login.php";


login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
request = new StringRequest(Request.Method.POST, loginUrl, new Response.Listener<String>() /*ERROR*/ {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response); /*ERROR*/
if (jsonObject.names().get(0).equals("success")) {
Toast.makeText(getApplicationContext(), jsonObject.getString("success"), Toast.LENGTH_SHORT).show();
email.setText("");
password.setText("");
Log.d("test", "Going to Main.class");
startActivity(new Intent(MainActivity.this, Main.class));
//setContentView(R.layout.activity_main);
} else {
Toast.makeText(getApplicationContext(), jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("email", email.getText().toString());
parameters.put("password", password.getText().toString());
return parameters;
}
};
requestQueue.add(request);
}
});

Answer

It seems like that the response object is not properly formatted to be converted into JSON object. Example.

Comments