【问题标题】:Android Studio / JAVA: How to trace error at android studio using "log"Android Studio / JAVA:如何使用“日志”在 android studio 中跟踪错误
【发布时间】:2020-04-05 09:32:39
【问题描述】:

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

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

【问题讨论】:

    标签: java android android-studio debugging


    【解决方案1】:

    在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)

    【讨论】:

    • 当我把 Log.d("onPostExecute","re​​sponse is: "s);在我的代码中,这个词出错了。为什么?
    • 如果没有在类声明上方添加import android.util.Log;,您是否添加了日志导入
    • 对不起,这是我的错,现在我更新了答案你可以再试一次吗?
    • 完成,之后。我如何追踪我的错误?我需要在哪里查看日志?
    • 您可以通过标签或消息中的任何字符串搜索,输入标签即。 logcat 搜索栏上的 onPostExecute
    猜你喜欢
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    相关资源
    最近更新 更多