responseLitsner内部缺少字段-Android Studio [英] Missing field inside responseLitsner - Android Studio

查看:53
本文介绍了responseLitsner内部缺少字段-Android Studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Volley在android中实现Login \ Register 和我使用JSONObject和Repose.listener,并且当我在监听器之外时,一切都很好,请在此处

hi guys im trying to implement Login\Register in android using Volley and im using JSONObject and Repose.listener, and when im outside the listener all is good ad show here

但在内部"侦听器的密码字段为丢失".如图此处

but im "inside" the listener the password field is "missing" as shown here

LoginActivity

LoginActivity

package com.example.ofir.bopofinal.LoginRegister;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import com.example.ofir.bopofinal.MainActivity;
import com.example.ofir.bopofinal.R;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        final EditText etUserName = (EditText) findViewById(R.id.etUserName);
        final EditText etPassword = (EditText) findViewById(R.id.etPassword);
        final Button bLogin = (Button) findViewById(R.id.bLogin);
        final TextView registerLink = (TextView) findViewById(R.id.tvRegisterHere);

        registerLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent regiserIntent = new Intent(LoginActivity.this,RegisterActivity.class);
                LoginActivity.this.startActivity(regiserIntent);
            }
        });

        bLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String username = etUserName.getText().toString();
                final String password = etPassword.getText().toString();

                Response.Listener<String> responseLitsner = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");
                            if (success){
                                String name = jsonResponse.getString("name");
                                int age = jsonResponse.getInt("age");

                                Intent loginIntent = new Intent(LoginActivity.this, MainActivity.class);
                                loginIntent.putExtra("name", name);
                                loginIntent.putExtra("username", username);
                                loginIntent.putExtra("age", age);
                                LoginActivity.this.startActivity(loginIntent);

                            }
                            else{
                                AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                                builder.setMessage("Login Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };

                LoginRequest loginRequest = new LoginRequest(username,password, responseLitsner);
                RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
                queue.add(loginRequest);
            }
        });
    }
}

LoginRequest

LoginRequest

package com.example.ofir.bopofinal.LoginRegister;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ofir on 11/17/2016.
 */
public class LoginRequest  extends StringRequest {


    private static final  String REGISTER_REQUEST_URL = "http://cellularguide.info/cellularguide.info/offir/Login.php";
    private Map<String,String> params;

    public LoginRequest(String username ,String password, Response.Listener<String> listener){
        super(Method.POST,REGISTER_REQUEST_URL,listener,null);
        params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);

    }
    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

Login.PHP

Login.PHP

<?php
   $con = mysqli_connect(....); // the con is working - dont worry
    
    $username = $_POST["username"];
    $password = $_POST["password"];
    
    $statement = mysqli_prepare($con, "SELECT username,password FROM user WHERE username = ? AND password = ?");
    mysqli_stmt_bind_param($statement, "ss", $username, $password);
    mysqli_stmt_execute($statement);
    
    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement,  $username, $password);
    
    $response = array();
    $response["success"] = false;  
    
    while(mysqli_stmt_fetch($statement)){
        $response["success"] = true;  
        $response["name"] = $name;
        $response["age"] = $age;
        $response["username"] = $username;
        $response["password"] = $password;
    }
    
    echo json_encode($response);
?>

此原因响应:始终为{"success":false}

this cause reponse: {"success":false} always

P.S 寄存器的工作方式相同(或多或少),并且工作正常

P.S the register works the same way (more or less) and it's working fine

推荐答案

我看到您打电话给响应,甚至没有打电话给登录名.它来自哪里?您似乎在甚至在bLogin.setOnClickListener中发送请求(LoginRequest)之前就调用了响应(Response.Listener).请尝试扭转它们.

I see you call a response, before even calling the Login. Where is it coming from? You seem to call the response (Response.Listener) before even sending the request (LoginRequest) in the bLogin.setOnClickListener. Please try to reverse them.

这篇关于responseLitsner内部缺少字段-Android Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆