I\'m having a problem in Android Studio on how to request JSON Object.
My Logcat can only print String onResponse but not JSONObject value. I\'m having a problem in lin
Problem :
As you can see in your logcat the response that you get from the server is success_access{"access":"PA001","password":"123","fullname":"ARCADE","branch":"HQ","section":"HQ"}
First, this is not a valid JSON
.
Also, that is the value of the response
variable.
Now, your response.equals("success_access")
will never return true, and hence the code control will never enter the if
block.
Solution :
Ideally, there should be no extra string like success_access
, insted your response should be a JSON
object ALWAYS containing one specified field (the result
feild for example). To show success, your JSON will look something like :
{
result : "success_access",
/* other parameters down here */
}
And in case of failure :
{
result : "access_failed"
}
And then your code should convert the respose
string into a JSON
(your server code should make sure that it returns a valid JSON always) check for the value of the result
field and proceed further.
Quick Fix :
Change your code to :
....
Log.e(TAG, "Response is = " + response);
String result = response.replace("{.*}","");
String jsonString = response.replace(result, "");
if(result.equals("success_access")){
try {
JSONObject jsonObject = new JSONObject(jsonString);
....