在Android中通过Rest完整API登录不起作用?
我是android新手,我开始通过Restful API登录,但我面临一个问题。点击登录按钮后,它不会打开下一页,也不会获取记录的用户详细信息 我正在使用显示已登录用户的详细信息另一个文件提供一个文本框id(textViewUsername),但它不起作用 登录权限代码在Android中通过Rest完整API登录不起作用?,android,mysql,Android,Mysql,我是android新手,我开始通过Restful API登录,但我面临一个问题。点击登录按钮后,它不会打开下一页,也不会获取记录的用户详细信息 我正在使用显示已登录用户的详细信息另一个文件提供一个文本框id(textViewUsername),但它不起作用 登录权限代码 public class LoginActivity extends AppCompatActivity implements{ public static final String LOGIN_URL ="http:/
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 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" />
公共类LoginActivity扩展了AppCompatActivity实现{
公共静态最终字符串登录\u URL=”http://email.php?email=%27%27&pass=%27%27";
公共静态最终字符串键\u EMAIL=“EMAIL”;
公共静态最终字符串密钥\u PASSWORD=“PASSWORD”;
私人编辑文本编辑电子邮件;
私人编辑文本编辑密码;
私人按钮按钮;
私人字符串电子邮件;
私有字符串密码;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
editTextemail=(EditText)findViewById(R.id.editTextUsername);
editTextPassword=(EditText)findViewById(R.id.editTextPassword);
buttonLogin=(按钮)findViewById(R.id.buttonLogin);
buttonLogin.setOnClickListener(此);
}
私有void userLogin(){
email=editTextemail.getText().toString().trim();
password=editTextPassword.getText().toString().trim();
StringRequest StringRequest=新的StringRequest(Request.Method.POST、LOGIN\u URL、,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
if(response.trim().equals(“success”)){
openProfile();
}否则{
Toast.makeText(LoginActivity.this、response、Toast.LENGTH_LONG.show();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(LoginActivity.this,error.toString(),Toast.LENGTH_LONG.show();
}
}){
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map Map=newhashmap();
地图放置(钥匙、电子邮件、电子邮件);
map.put(KEY_密码,PASSWORD);
返回图;
}
};
RequestQueue RequestQueue=Volley.newRequestQueue(this);
添加(stringRequest);
}
私有void openProfile(){
意向意向=新意向(此,ActivityUserProfile.class);
意向。额外(钥匙、电子邮件、电子邮件);
星触觉(意向);
}
@凌驾
公共void onClick(视图v){
userLogin();
/*如果(v==按钮登录){
startActivity(新的意图(这个,ActivityUserProfile.class));
}*/}}
xml活动\u登录代码
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 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" />
**活动登录的xml代码**
是记录还是登录?
假设在这种情况下,您必须使用以下日志:
@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 Monitor中检查将出现什么响应,也可以在onErrorListner()中检查。您是否在清单文件中设置了Internet连接权限():PAlso,你在日志中看到了什么错误消息?@mike我在清单文件中添加了INTERNET权限,但没有显示错误日志id。。。点击登录按钮后,它不会显示任何内容!!!您上面的代码片段显然没有,但您确定LoginActivity实现了View.OnClickListener吗?@Harish mittal如果单击loin按钮它没有显示任何内容,则login按钮不工作。。。!!选中@Override public void OnClick(视图){switch(View.getId()){case R.id.buttonLogin:userLogin();break;}