【问题标题】:Android Volley php mysqlAndroid Volley php mysql
【发布时间】:2016-09-23 08:33:54
【问题描述】:

我阅读了this 教程。但是当按下登录或注册时我遇到问题Button 不显示来自StringRequest 的消息。

StringRequest stringRequest = new StringRequest(Request.Method.POST, login_url,
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONArray jsonArray = new JSONArray(response);
                JSONObject jsonObject = jsonArray.getJSONObject(0);
                String code = jsonObject.getString("code");
                if(code.equals("login_failed")){
                    builder.setTitle("Login Error");
                    displayAlert(jsonObject.getString("message"));
                }

                else {
                    //Intent intent = new Intent(MainActivity.this, LoginSuccess.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("name", jsonObject.getString("name"));
                    bundle.putString("email", jsonObject.getString("email"));
                    startActivity(new Intent(MainActivity.this, LoginSuccess.class).putExtras(bundle));
                    //intent.putExtras(bundle);
                    //startActivity(intent);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_LONG).show();
            error.printStackTrace();
        }
    }
);

【问题讨论】:

  • 您检查响应是否收到有效响应?
  • 你是否将 HashMap 值传递给 API 过去?

标签: android json android-volley


【解决方案1】:

试试这个:

RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
        String url = "login_url";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                try {
                    JSONArray jsonArray = new JSONArray(response);
                    JSONObject jsonObject = jsonArray.getJSONObject(0);
                    String code = jsonObject.getString("code");
                    if(code.equals("login_failed")){
                        builder.setTitle("Login Error");
                        displayAlert(jsonObject.getString("message"));
                    }

                    else {
                        //Intent intent = new Intent(MainActivity.this, LoginSuccess.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("name", jsonObject.getString("name"));
                        bundle.putString("email", jsonObject.getString("email"));
                        startActivity(new Intent(MainActivity.this, LoginSuccess.class).putExtras(bundle));
                        //intent.putExtras(bundle);
                        //startActivity(intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_LONG).show();
                error.printStackTrace();

            }
        }) {
            protected Map<String, String> getParams() {

                Map<String, String> MyData = new HashMap<String, String>();
                String userName = edtUserName.getText().toString();
                String password = edtPassword.getText().toString();
                if (userName.length() > 0 && password.length() > 0) {

                    MyData.put("username", userName);
                    MyData.put("password", password);

                }

                return MyData;
            }

        };
        MyRequestQueue.add(MyStringRequest);

【讨论】:

    猜你喜欢
    • 2017-03-28
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 2020-04-22
    • 1970-01-01
    • 2021-03-11
    相关资源
    最近更新 更多