2017-05-05 73 views
0

我是新来的android我开始通过Restful API登录,但我面临一个问题。在录入登录按钮后,它不会打开下一页,也不会获取记录的用户详细信息。在Android中通过Rest Full API登录不起作用?

我正在使用显示记录的用户的另一个文件的详细信息给文本框ID为(textViewUsername),但它不工作。

LoginAcitivity代码

public class LoginActivity extends AppCompatActivity implements{ 
    public static final String LOGIN_URL ="http://email.php?email=%27%27&pass=%27%27"; 

    public static final String KEY_EMAIL="email"; 
    public static final String KEY_PASSWORD="password"; 

    private EditText editTextemail; 
    private EditText editTextPassword; 
    private Button buttonLogin; 

    private String email; 
    private String password; 

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

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

     buttonLogin = (Button) findViewById(R.id.buttonLogin); 
     buttonLogin.setOnClickListener(this); 
    } 

    private void userLogin() { 
     email = editTextemail.getText().toString().trim(); 
     password = editTextPassword.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(LoginActivity.this,response,Toast.LENGTH_LONG).show(); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(LoginActivity.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_EMAIL,email); 
      map.put(KEY_PASSWORD,password); 
      return map; 
     } 
    }; 

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

private void openProfile(){ 
    Intent intent = new Intent(this, ActivityUserProfile.class); 
    intent.putExtra(KEY_EMAIL, email); 
    startActivity(intent); 
} 

@Override 
public void onClick(View v) { 
    userLogin(); 
    /* if(v == buttonLogin){ 
     startActivity(new Intent(this,ActivityUserProfile.class)); 
}*/}} 

XML activity_login代码

**xml Code for activity_login** 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:orientation="vertical" 
android:layout_height="match_parent" 
tools:context="LoginActivity"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Enter Email" 
    /> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/editTextUsername" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Enter Password" 
    /> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/editTextPassword" /> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Login" 
    android:id="@+id/buttonLogin" /> 

+0

您是否拥有在Manifest文件中设置的Internet连接权限( )? :P – Mike

+0

另外,您在日志中看到的错误消息是什么? – Mike

+0

@mike我在清单文件中添加了INTERNET权限,并且没有显示错误日志ID ...点击登录按钮后它没有显示任何东西! –

回答

0

它是记录或登录? 假设日志记录这种情况下,你必须使用:

@Override 
public void onResponse(String response) { 
// Log.e(TAG,response); or Log.v(TAG,response) 
    if (response.trim().equals("success")) { 
     openProfile(); 
    } else { 
     Toast.makeText(LoginActivity.this, response, Toast.LENGTH_LONG).show(); 
    } 
} 

,您可以在Android的监控检查什么应对即将到来,也onErrorListner检查()。

+0

@Harish Mittal它是用于登录的! –

+0

@Harish mittal如果单击腰部按钮它不显示任何东西,登录按钮不工作...! –

+0

检查 @Override public void OnClick(View view){ switch(view.getId()){ case R.id.buttonLogin: userLogin(); 休息; } } –