使用Android Volley

时间:2017-09-08 03:55:52

标签: android web-services android-volley

我是android新手,我使用PHP创建了一个带登录功能的简单Web服务器。将参数从Android传递到此服务器时始终接收null。 以下是我的代码。

我的java类连接到我的在线服务器:

public class LoginActivity extends AppCompatActivity  {

private  EditText userNameEd,passwordED;
private  Button   login;
private String LOGIN_URL = "http://404-76-01.com/appLogin.php";
public static String KEY_USERNAME = "username";
public static String KEY_PASSWORD = "password";
private String srtUsername,srtPassword;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    userNameEd = (EditText) findViewById(R.id.username);
    passwordED = (EditText) findViewById(R.id.password);
    login    = (Button)   findViewById(R.id.signin);

    login.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

           srtUsername = userNameEd.getText().toString();
           srtPassword = passwordED.getText().toString();
            //Toast.makeText(LoginActivity.this, "This is my Toast message!"+srtUsername,
                    //Toast.LENGTH_LONG).show();
           // WebService toLogin = new WebService();
                   // toLogin.login(srtUsername,srtPassword);

            login(srtUsername,srtPassword);
        }
    });
}


public void login(final String username, final String Password){
    StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                   if (response.equals("123")) {
                        callToMenu();
                        Toast.makeText(LoginActivity.this,srtPassword +"/t" +srtUsername ,
                                Toast.LENGTH_LONG).show();

                    } else {
                       //Toast.makeText(login, response, Toast.LENGTH_LONG).show();
                        Toast.makeText(LoginActivity.this, srtPassword +"/t" +srtUsername,
                        Toast.LENGTH_LONG).show();
                        //callToMenu();
                    }
                    Toast.makeText(LoginActivity.this,response.trim() ,
                            Toast.LENGTH_LONG).show();

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //Toast.makeText(this,error.toString(),Toast.LENGTH_LONG ).show();
                }
            }
    ){

        protected Map<String,String> getPaaram() throws AuthFailureError {

            Map<String,String> map = new HashMap<String, String>();
            map.put(KEY_USERNAME,username);
            map.put(KEY_PASSWORD,Password);
            return map;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}


private  void callToMenu(){
    Intent menu = new Intent(LoginActivity.this,Inside.class);
    startActivity(menu);
}
}

我的PHP代码: 当我将值传递给此PHP页面时,$ password和$ username始终为null。我无法找到我的代码有什么问题。

<?php
require_once('Dbconnect.php');
$obj = new Dbconnection();

 if($_SERVER['REQUEST_METHOD']=='POST'){
$username = $_POST["username"];
$password = $_POST["password"];    
$count  = 0;        
$sql = "select * from employee where username = '".$username."' and  PASSWORD  ='".$password."'";    
$result_val = mysqli_query($obj->getdbconnect(),$sql);    
while($rs = mysqli_fetch_array($result_val)){    
        $usname = $rs["username"];
$count++;    
}

var_dump($username,$password);die;
if($count>0){
echo "123";
}else{
echo "fail";
}
}

谢谢

2 个答案:

答案 0 :(得分:2)

在您的代码中更改此内容:getParams应为@override anotation

new_dict = {k:dictionary[k] for k in dictionary if re.match(regex, k)}

答案 1 :(得分:1)

调试您的应用以确保您将有效值传递到网络堆栈。 使用Charles之类的工具查看传出请求,并验证您的问题是在客户端还是服务器中。祝你好运。