Reputation: 19
Currently, I have a problem with my code. An error displayed as follows
Value br of type java.lang.String cannot be converted to JSONObject
I try to find if there a problem at my PHP side, but I also dunno how to trace the problem.
Thus, can I know how to use the log
and where I need to put the log
in my code?
I hope anyone can help me with this. Thanks
Below is my current code
public class MainActivity extends AppCompatActivity {
EditText etBadgeid, etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etBadgeid = findViewById(R.id.etBadgeid);
etPassword = findViewById(R.id.etPassword);
findViewById(R.id.btnLogin).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userLogin();
}
});
}
private void userLogin() {
final String badgeid = etBadgeid.getText().toString();
final String pwd = etPassword.getText().toString();
class UserLogin extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
//converting response to json object
JSONObject obj = new JSONObject(s);
//if no error in response
if (!obj.getBoolean("error")) {
Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
//getting the user from the response
JSONObject userJson = obj.getJSONObject("user");
//creating a new user object
User user = new User(
userJson.getString("badgeid"),
userJson.getString("email"),
userJson.getString("fullname"),
userJson.getInt("roles_id"),
userJson.getInt("team_id")
);
//storing the user in shared preferences
SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);
//starting the profile activity
finish();
startActivity(new Intent(getApplicationContext(), Home.class));
} else {
Toast.makeText(getApplicationContext(), "Invalid username or password", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
//creating request handler object
RequestHandler requestHandler = new RequestHandler();
//creating request parameters
HashMap<String, String> params = new HashMap<>();
params.put("badgeid", badgeid);
params.put("pwd", pwd);
//returing the response
return requestHandler.sendPostRequest(URLs.URL_LOGIN, params);
}
}
UserLogin ul = new UserLogin();
ul.execute();
}
@Override
public void onBackPressed() {
finish();
System.exit(0);
}
}
Upvotes: 1
Views: 228
Reputation: 863
In android you can log with Log.d(tag,message)
, and you can view the debug message in the logcat
You should print log before parsing the response string,
...
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("onPostExecute","response is: "+s);
try {
//converting response to json object
JSONObject obj = new JSONObject(s);
....
Your response is probably a non json formatted plain text.
for more info https://developer.android.com/reference/android/util/Log.html#d(java.lang.String,%20java.lang.String)
Upvotes: 2