Volly互联网连接错误

时间:2016-06-23 11:27:10

标签: android

I want to test login is successful or not but when I click login button it gives the small popup with error. what will be solution?? package com.example.txisandroid.demonstration;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

public static final String LOGIN_URL = "http://localhost/phpdata.php";

public static final String KEY_USERNAME="username";
public static final String KEY_PASSWORD="password";
public static final String KEY_IMEINUM="imeinum";
enter code here

private EditText editTextUsername;
private EditText editTextPassword;
private EditText editText;
private Button buttonLogin;

private String username;
private String password;
private String imeinum;

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

    editTextUsername = (EditText) findViewById(R.id.editTextUsername);
    editTextPassword = (EditText) findViewById(R.id.editTextPassword);
    editText = (EditText) findViewById(R.id.editText);

    buttonLogin = (Button) findViewById(R.id.buttonLogin);

    buttonLogin.setOnClickListener(this);
}


private void userLogin() {
    username = editTextUsername.getText().toString().trim();
    password = editTextPassword.getText().toString().trim();
    imeinum = editText.getText().toString().trim();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if(response.trim().equals("success")){
                        openProfile();
                    }else{
                        Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_LONG).show();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG ).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> map = new HashMap<String,String>();
            map.put(KEY_USERNAME,username);
            map.put(KEY_PASSWORD,password);
            map.put(KEY_IMEINUM,imeinum);
            return map;
        }
    };

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

}

private void openProfile(){
    Intent intent = new Intent(this, userprofile.class);
    intent.putExtra(KEY_USERNAME, username);
    startActivity(intent);
}

@Override
public void onClick(View v) {
    userLogin();
}
}

1 个答案:

答案 0 :(得分:0)

我认为onClick方法应该是:

@Override
public void onClick(View v) {
    if(v.getId() == R.id.buttonLogin){
        userLogin();   
    }
}

注意:如果您有许多按钮,请使用switch-case而不是使用IF-else。