2016-06-23 10 views
-2

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;Vollyインターネット接続エラー

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

注:あなたは多くのボタンがある場合は使用ではなく、IF-ELSEを使用するケースを切り替える

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(); 
} 
} 
+0

何度も尋ねられます... ** 127.0.0.1/localhostはデバイス/エミュレータ自体です** ...あなたのバックエンドはデバイスにありますか? – Selvin

+0

マニフェストにインターネット権限を追加します。 –

答えて

0

私はのonClickメソッドがあるべきだと思います。

関連する問題