簡體   English   中英

Android Studio / JAVA:如何使用“日志”跟蹤 android studio 中的錯誤

[英]Android Studio / JAVA: How to trace error at android studio using "log"

目前,我的代碼有問題。 錯誤顯示如下

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM