I have got a JSON response from an API that I have converted to a string and trying to compare it for true or false value, On the log cat I can see the result:
{
"message": "success",
"status": "Auth_Successful",
"response": "Authentication successful"
}
I am trying fit it into an if statement like below
I have tried most of the comparison methods(==, .equals(), .compareTo()) but not getting any result
Can anyone let me know what is the correct way to approach it as I am still new to Java and Android. I have seen a lot of similar posts but unable to figure out.
Thank you very much for your time and assistance in this matter.
package com.example.shiben.fabapp;
public class MainActivity extends AppCompatActivity {
private Request request;
private static final String Tag = MainActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button LoginButton = (Button) findViewById(R.id.loginButton);
EditText userName = (EditText) findViewById(R.id.userName);
EditText userPassword = (EditText) findViewById(R.id.userPassword);
final TextView displayTest = (TextView) findViewById(R.id.displayTest);
LoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "username=xxxxxxxxx&password=xxxxxxxxx");
Request request = new Request.Builder()
.url("http://9.xxx.xxx.xxx/test/xxxxx_api.aspx")
.post(body)
.addHeader("cache-control", "no-cache")
.addHeader("content-type", "application/json")
.build();
//execute the request
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i(Tag, e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i(Tag, response.body().string());
String result = response.body().toString();
//if (result==("{\"message\":\"success\",\"status\":\"Auth_Successful\",\"response\":\"Authentication successful\"}")){
if (result.compareTo("{\"message\":\"success\",\"status\":\"Auth_Successful\",\"response\":\"Authentication successful\"}")==0) {
//if (result.equals("{\"message\":\"success\",\"status\":\"Auth_Successful\",\"response\":\"Authentication successful\"}")) {
TastyToast.makeText(MainActivity.this, "String Comparison Success", TastyToast.LENGTH_LONG, TastyToast.SUCCESS);
}
}
});
}
});
}
}