输入正确的登录数据后Logcat显示登录错误

在 LoginActivity.java 中输入正确数据后,应用程序不会进入下一个活动,进度条不断加载,Logcat 向我显示此“登录”:[],“成功”:“0”,“消息”:“错误”当我在响应中使用Log.d时,看起来我插入了不正确的数据,但我确信它是正确的。我找不到问题出在哪里,我还在学习。


下面是一段代码:


  private void Login(final String email, final String password){

    loading.setVisibility(View.VISIBLE);

    btn_login.setVisibility(View.GONE);

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_LOGIN,

            new Response.Listener<String>() {

                @Override

                public void onResponse(String response) {

                    try {

                        JSONObject jsonObject = new JSONObject(response);

                        String success = jsonObject.getString("success");

                        JSONArray jsonArray = jsonObject.getJSONArray("login");


                        if(success.equals("1")){

                            for (int i = 0; i < jsonArray.length(); i++){


                                JSONObject object = jsonArray.getJSONObject(i);


                                String name = object.getString("name").trim();

                                String email = object.getString("email").trim();


                                Intent intent = new Intent(LoginActivity.this, HomeActivity.class);

                                intent.putExtra("name", name);

                                intent.putExtra("email", email);

                                startActivity(intent);


                                loading.setVisibility(View.GONE);

                            }

                        }

                    } catch (JSONException e) {

                        e.printStackTrace();

                        loading.setVisibility(View.GONE);

                        btn_login.setVisibility(View.VISIBLE);

                        Toast.makeText(LoginActivity.this, "Error " +e.toString(), 

    Toast.LENGTH_SHORT).show();

                    }

                    Log.d(TAG, "Info" + response);

                }

            },



慕侠2389804
浏览 111回答 2
2回答

繁星淼淼

StringRequest用于GET请求并发出POST请求,请使用JsonObjectRequest。将用户名和密码发送到服务器的示例发布请求。private void login(final String email, final String password) {&nbsp; &nbsp; loadingBar.setVisibility(View.VISIBLE);&nbsp; &nbsp; loginButton.setVisibility(View.GONE);&nbsp; &nbsp; JSONObject jsonObject = new JSONObject();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; jsonObject.put("username", email);&nbsp; &nbsp; &nbsp; &nbsp; jsonObject.put("password", password);&nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL_LOGIN,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObject, new Response.Listener<JSONObject>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(JSONObject response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String success = response.getString("success");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonArray = response.getJSONArray("login");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (success.equals("1")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < jsonArray.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject object = jsonArray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name = object.getString("name").trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String email = object.getString("email").trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(LoginActivity.this,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MainActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra("name", name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra("email", email);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loadingBar.setVisibility(View.GONE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loadingBar.setVisibility(View.GONE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loginButton.setVisibility(View.VISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(LoginActivity.this, "Error " + e.toString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "Info" + response);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }, new Response.ErrorListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onErrorResponse(VolleyError error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loadingBar.setVisibility(View.GONE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loginButton.setVisibility(View.VISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(LoginActivity.this, "Error " + error.toString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }) {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public Map<String, String> getHeaders() throws AuthFailureError {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> params = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params.put("Content-Type", "application/json");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return params;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; // Make the request&nbsp; &nbsp; Volley.newRequestQueue(this).add(jsonObjectRequest);}

SMILET

您的登录操作似乎没有成功。当你得到 0 表示成功"login":[],"success":"0","message":"error"你应该得到"success":"1"。所以,我想你需要再次检查你的数据。我将向您推荐几点,它们将在处理 API 时为您提供帮助始终使用POSTMAN检查您的 API 请求/响应使用模型来表示您的响应,而不是JSONObject,&nbsp;JSONArray,..ext。使用简单且自定义的 http/https 库。&我会推荐这个快乐编码
打开App,查看更多内容
随时随地看视频慕课网APP