字符串请求未在齐射

时间:2017-05-16 12:39:08

标签: php android android-volley

我想使用Volley进行登录,但我的请求中没有数据。没有错误所以我不知道什么是错的。我在服务器上有一个PHP脚本,它返回一个json字符串并且工作正常。

以下是代码:

if(isset($_POST['correo'], $_POST['contrasenya']))
{
        $ecorreo = $_POST['correo'];
        $pwd = $_POST{'contrasenya'};

        if(!empty($ecorreo) && !empty($pwd))
        {
            $pwd_encriptada = md5($pwd);
            $this -> vresul = $this ->  usuario_existe($ecorreo,  
             $pwd_encriptada);

        }
        else
            $this -> vresul = "All fields must be filled";
    }
    else
        $this -> vresul = "Empty fields";   

    return $this -> vresul; //the json string returned
}

public function usuario_existe($prcorreo,$prcontrasenya)
{
    $this -> qry = "select * from usuarios where correo = ? and contrasenya = ?";
    $this -> sqry = $this -> conexion -> prepare($this -> qry);
    $this -> sqry -> execute(array($prcorreo,$prcontrasenya));

    $this -> result = $this -> sqry -> fetch(PDO::FETCH_ASSOC);

    $this -> conexion = NULL;


    if(!empty($this -> result)) 
    {
        $this -> datos['correo'] = utf8_encode($this -> result['correo']);
        $this -> datos['contrasenya'] = utf8_encode($this -> result['contrasenya']);    

        $this -> resultado = json_encode($this -> datos);
    }
    else 
            $this -> resultado = "Incorrect user or password";

    return $this -> resultado;
}

}

android代码:

final String correo = entcorreo.getText().toString().trim();
final String contrasena = entcontrasenya.getText().toString().trim();

    RequestQueue requestQueue = Volley.newRequestQueue(LoginTienda.this);

    StringRequest stringRequest = new StringRequest(Request.Method.POST,  
    Config.LOGIN_URL,new Response.Listener<String>()
         {
            @Override
            public void onResponse(String response)
            {

              Toast.makeText(LoginTienda.this,response,
               Toast.LENGTH_LONG).show(); //response is blank
             }

           },

            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError Error)
                {

                  Snackbar.make(findViewById(android.R.id.content), 
                  "Usuario o contraseña incorrectos",   
                   Snackbar.LENGTH_LONG);
                            .setAction("Action", null).show();
                }
            });  

在应用程序中正确键入用户和密码后,响应为空。 我怎样才能正确得到答案?

感谢。

3 个答案:

答案 0 :(得分:1)

对于Android:

StringRequest request = new StringRequest(Request.Method.POST, YourUrl, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        if (!response.equals(null)) {
            Log.e("Your Array Response", response);                    
        } else {
            Log.e("Your Array Response", "Data Null");
        }
    }

}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e("error is ", "" + error);
    }
}) {    

 //This is for Headers If You Needed
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> params = new HashMap<String, String>();
        params.put("Content-Type", "application/json; charset=UTF-8");
        params.put("token", ACCESS_TOKEN);
        return params;
    }

 //Pass Your Parameters here
    @Override
    protected Map<String, String> getParams() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("User", UserName);
        params.put("Pass", PassWord);
        return params;
    }
};
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
queue.add(request);

答案 1 :(得分:0)

在RequestQueue下添加以下行: - requestQueue.add(stringRequest);

答案 2 :(得分:0)

您是否尝试在响应对象上使用toString()方法?

        @Override
        public void onResponse(String response)
        {

          try {
                JSONObject jObj = new JSONObject(response);
                Toast.makeText(LoginTienda.this,response.toString(),
                Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();

        }

或者您可以尝试从onResponse回调中调用您的祝酒词,例如:

        @Override
        public void onResponse(String response) {

            responseTxt = "our response: = " +response;
            showToast();

        }

        //(...)

        private void showToast() {

        Toast.makeText(LoginTienda.this, responseTxt, Toast.LENGTH_SHORT).show();
        }

如果要从php文件中提取JSON值,则必须创建JSONObject。试试这个:

        @Override
        public void onResponse(String response) {

            try {
                JSONObject jObj = new JSONObject(response);
                    String correo = jObj.getString("correo"); //1st value
                    String contrasenya = jObj.getString("contrasenya"); //2nd value


            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }

         }