布尔检查的值在&#34处为空;返回检查"

时间:2017-02-28 06:15:36

标签: php android boolean localhost android-volley

我在本地主机中使用齐射连接,并在本地主机中有一个php文件。下面是我的VolleyConnectionLogin类,我还有一个凌空单例类(没有错误)..

public class VolleyConnectionLogin {

    Context context;
    public VolleyConnectionLogin(Context context){
        this.context = context;
    }

    static Boolean check;
    public Boolean volleyConnection(final String username , final String password) {
        String tag_string_req = "string_req";
        String url = "http://192.168.10.8/login/forlogin.php";
        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, response.toString());
                        if(response.toString().equals("successful")){
                            check = true;
                        }else{
                            check = false;
                        }
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                check = false;
                VolleyLog.d(TAG, "Error: " + error.getMessage());
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("uname", username);
                params.put("pword", password);
                return params;
            }

        };
        VolleySingleton.getInstance(context).addToRequestQueue(stringRequest);
        return check;
    }
}

2 个答案:

答案 0 :(得分:1)

check为空,因为请求为asynchronous,因此在调用以下行时

VolleySingleton.getInstance(context).addToRequestQueue(stringRequest);

程序然后进入下一行

return check; // (which is still null) 

当后台的另一个线程开始处理请求时。

您可以modify the request to be synchronous或修改您的代码,以便根据此方法的返回,您所拥有的任何内容都会在onResponse()回调的Response.Listener块中发生。

答案 1 :(得分:1)

因为你在接收响应的线程之外返回布尔值,所以

 Log.d(TAG, response.toString());
                        if(response.toString().equals("successful")){
                            check = true;
                        }else{
                            check = false;
                        }
                    }
在响应返回之前,

控制转到return check;

相关问题