2017-05-05 41 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(); 休息; } } –