响应侦听器无法正常工作

时间:2017-02-09 21:22:18

标签: java android listener response

我开始编写一个Android应用程序,我基本上是使用Volley进行登录以连接到数据库。

问题是,在点击登录按钮后,它似乎没有进入onResponse方法。

登录活动

import com.android.volley.RequestQueue;

import com.android.volley.Response;

import com.android.volley.toolbox.Volley;

import com.google.android.gms.appindexing.Action;

import com.google.android.gms.appindexing.AppIndex;

import com.google.android.gms.appindexing.Thing;


import com.google.android.gms.common.api.GoogleApiClient;

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 username = (EditText) findViewById(R.id.username);
    final EditText password = (EditText) findViewById(R.id.password);
    final Button bLogin = (Button) findViewById(R.id.bLogin);


    bLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final String user = username.getText().toString();
            final String psw = password.getText().toString();


            Response.Listener<String> responseListener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");

                        if (success) {

                            String user = jsonResponse.getString("username");
                            Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class);
                            intent.putExtra("user", user);

                            LoginActivity.this.startActivity(intent);



                        } 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(user, psw, responseListener);
            RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
            queue.add(loginRequest);



        }
    });

}}

这是我的登录请求

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

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


public class LoginRequest extends StringRequest{

private static final String LOGIN_REQUEST_URL = "http://localhost/loginApp.php";
private Map<String, String> params;


public LoginRequest(String user, String psw, Response.Listener<String> listener){

    super(Method.POST, LOGIN_REQUEST_URL, listener, null);
    params = new HashMap<>();
    params.put("username", user);
    params.put("password", psw);
}

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

0 个答案:

没有答案