Android Studio / JAVA:如何使用“ log”跟踪Android Studio的错误

时间:2019-12-12 02:50:16

标签: java android android-studio debugging

当前,我的代码有问题。错误显示如下

Value br of type java.lang.String cannot be converted to JSONObject

我尝试查找PHP方面是否存在问题,但我也不知道如何跟踪问题。

因此,我可以知道如何使用log以及我需要在代码中放置log的地方吗?

我希望有人可以帮助我。谢谢

下面是我当前的代码

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);
    }
}

1 个答案:

答案 0 :(得分:2)

在android中,您可以使用Log.d(tag,message)登录,并且可以在logcat中查看调试消息

您应该在解析响应字符串之前打印日志

...
@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);
....

您的回复可能是非json格式的纯文本。

了解更多信息 https://developer.android.com/reference/android/util/Log.html#d(java.lang.String,%20java.lang.String)