我需要一些帮助。这是我的活动登录信息,单击登录按钮后,他给我一个错误:com.android.volley.NoConnectionError:java.net.ConnectException:Connection refused
。我使用2个权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
有我的登录代码,在顶部有变量:
private EditText mPasswordView, mUserName;
private ProgressBar loading;
private Button btnLogin;
private static String URL_LOGIN = "http://localhost/ligacao_bd/login.php";
private View mProgressView;
private View mLoginFormView;
有我的按钮登录设置:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loading = findViewById(R.id.loading);
// Set up the login form.
mUserName = (EditText) findViewById(R.id.textUserName);
mPasswordView = (EditText) findViewById(R.id.textPassword);
btnLogin = (Button) findViewById(R.id.btnLogin); // login button
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String email = mUserName.getText().toString().trim();
String Pwd = mPasswordView.getText().toString().trim();
if(!email.isEmpty() || !Pwd.isEmpty()){
Login(email, Pwd);
}else{
mUserName.setError("Insira nome de utilizador");
mPasswordView.setError("Insira palavra-passe");
}
}
});
还有我的JSON登录功能,包括:
private void Login(final String email, final String password) {
loading.setVisibility(View.VISIBLE);
btnLogin.setVisibility(View.GONE);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_LOGIN,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("Sucesso");
JSONArray jsonArray = jsonObject.getJSONArray("login");
if(success.equals("1")) {
for (int i = 0; i < jsonArray.length(); i++){
JSONObject object = jsonArray.getJSONObject(i);
String nome = object.getString("nome").trim();
String email = object.getString("email").trim();
Toast.makeText(LoginActivity.this, "Login efetuado com sucesso\n Bem-Vindo " +nome, Toast.LENGTH_SHORT).show();
loading.setVisibility(View.GONE);
}
}
}catch (JSONException e){
e.printStackTrace();
loading.setVisibility(View.GONE);
btnLogin.setVisibility(View.VISIBLE);
Toast.makeText(LoginActivity.this, "Erro " + e.toString(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loading.setVisibility(View.GONE);
btnLogin.setVisibility(View.VISIBLE);
Toast.makeText(LoginActivity.this, "Erro " + error.toString(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("password", password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
答案 0 :(得分:0)
您还需要在代码中请求权限。参见:https://developer.android.com/training/permissions/requesting
您只需要在发出Web请求之前添加以下代码:
requestPermission(Manifest.permission.INTERNET, PERMISSION_INTERNET);
requestPermission(Manifest.permission.ACCESS_NETWORK_STATE, PERMISSION_ACCESS_NETWORK_STATE);
此代码的其他地方:
private static final int PERMISSION_INTERNET = 1;
private static final int PERMISSION_ACCESS_NETWORK_STATE = 2;
private void requestPermission(Manifest.permission permission, int requestId) {
if (ContextCompat.checkSelfPermission(thisActivity,
permission)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(thisActivity,
new String[]{permission},
requestId);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_INTERNET: {
if (grantResults.length <= 0
|| grantResults[0] != PackageManager.PERMISSION_GRANTED) {
requestPermission(Manifest.permission.INTERNET, PERMISSION_INTERNET);
}
return;
}
case PERMISSION_ACCESS_NETWORK_STATE: {
if (grantResults.length <= 0
|| grantResults[0] != PackageManager.PERMISSION_GRANTED) {
requestPermission(Manifest.permission.ACCESS_NETWORK_STATE, PERMISSION_ACCESS_NETWORK_STATE);
}
return;
}
}
}
答案 1 :(得分:0)
假设您正在Android模拟器上运行此代码,并且您要调用的后端服务也在本地主机上运行,请将您的登录地址更改为:
//8080 being the port that localhost is assigned to - change it if needs be
private static String URL_LOGIN = "http://10.0.2.2:8080/ligacao_bd/login.php";
http://10.0.2.2:xxxx是IP,它是主机回送接口的特殊别名(开发机上为127.0.0.1)
答案 2 :(得分:0)
这是使用截击的工作字符串请求。根据需要更改此设置。如果不起作用,则问题出在您的login.php端
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>(){
@Override
public void onResponse(String s) {
if(s.equals("Logged In")){
Intent intent = new Intent(Login.this, Home.class);
startActivity(intent);
}
else{
Toast.makeText(Login.this, "Incorrect Details", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(Login.this, "Some error occurred -> "+volleyError, Toast.LENGTH_LONG).show();;
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("email", emailBox.getText().toString());
parameters.put("password", passwordBox.getText().toString());
return parameters;
}
};
RequestQueue rQueue = Volley.newRequestQueue(Login.this);
rQueue.add(request);
}
});