gpt4 book ai didi

java - com.android.volley.NoConnectionError : java.net.ConnectException:连接被拒绝

转载 作者:行者123 更新时间:2023-12-02 10:13:52 28 4
gpt4 key购买 nike

我需要一些帮助。这是我的 Activity 登录,当单击登录按钮时,他给我一个错误:com.android.volley.NoConnectionError:java.net.ConnectException:Connection returned。我使用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);

}

最佳答案

假设您在 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,是主机环回接口(interface)的特殊别名(开发计算机上的 127.0.0.1)

关于java - com.android.volley.NoConnectionError : java.net.ConnectException:连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54810579/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com