我正在使用Volley向我服务器上的login.php发送登录请求。当我单击登录时,它应该给出错误或登录。我遇到的问题是它显示进度对话框并且什么都不做,进度对话框永远不会消失。任何人都知道可能是什么问题?
package com.application.application;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class MemberLogin extends AppCompatActivity {
private EditText email, password;
private RequestQueue requestQueue;
private static final String URL = "http://www.example.com/login.php";
private StringRequest request;
ProgressDialog progressDialog;
UserLocalStore userLocalStore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_member_login);
userLocalStore = new UserLocalStore(this);
email = (EditText) findViewById(R.id.etEmail);
password = (EditText) findViewById(R.id.etPassword);
Button login = (Button) findViewById(R.id.btnLogin);
Button cancel = (Button) findViewById(R.id.btnCancel);
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
progressDialog.setTitle("Processing");
progressDialog.setMessage("Please wait...");
email.requestFocus();
requestQueue = Volley.newRequestQueue(this);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MemberLogin.this, MemberAccess.class));
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.show();
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.names().get(0).equals("success")) {
User user = new User(email.getText().toString(), password.getText().toString());
Toast.makeText(MemberLogin.this, jsonObject.getString("success"), Toast.LENGTH_SHORT).show();
userLocalStore.setUserLoggedIn(true);
userLocalStore.storeUserData(user);
startActivity(new Intent(MemberLogin.this, MemberHome.class));
} else {
progressDialog.dismiss();
Toast.makeText(MemberLogin.this, jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
password.setText("");
if (email.getText().toString().equals("")) {
email.requestFocus();
} else {
password.requestFocus();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("email", email.getText().toString());
hashMap.put("password", password.getText().toString());
progressDialog.dismiss();
return hashMap;
}
};
requestQueue.add(request);
}
});
}
}
答案 0 :(得分:0)
您需要像这样更新此方法:
URL url = new URL("https://www.android.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(5000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
urlc.disconnect();
return true;
}
答案 1 :(得分:0)
可能是您无法访问对话框的地方。
protected Map<String, String> getParams() throws AuthFailureError
检查您需要关闭所有对话框的所有其他方案和地点。
一个建议是设置对话框Cancelable为true,即progressDialog.setCancelable(false);至少,用户可以选择取消和关闭应用程序。
答案 2 :(得分:0)
编辑您的答案如下:
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.show();
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.names().get(0).equals("success")) {
User user = new User(email.getText().toString(), password.getText().toString());
Toast.makeText(MemberLogin.this, jsonObject.getString("success"), Toast.LENGTH_SHORT).show();
userLocalStore.setUserLoggedIn(true);
userLocalStore.storeUserData(user);
startActivity(new Intent(MemberLogin.this, MemberHome.class));
} else {
Toast.makeText(MemberLogin.this, jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
password.setText("");
if (email.getText().toString().equals("")) {
email.requestFocus();
} else {
password.requestFocus();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) { progressDialog.dismiss(); }}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("email", email.getText().toString());
hashMap.put("password", password.getText().toString());
progressDialog.dismiss();
return hashMap;
}
};
requestQueue.add(request);
}
});
}
}