com.android.volley.NoConnectionError:

我需要一些帮助。这是我的活动登录,当点击登录按钮时,他给了我一个错误: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");

            }

        }

    });



蓝山帝景
浏览 219回答 3
3回答

哔哔one

假设您在 Android 模拟器上运行此代码,并且您调用的任何后端服务也在 localhost 上运行,请将您的登录地址更改为://8080 being the port that localhost is assigned to - change it if needs beprivate 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)

海绵宝宝撒

这是一个使用 volley 的工作字符串请求。根据需要更改此设置。如果它不起作用,那么问题出在您的 login.php 方面loginButton.setOnClickListener(new View.OnClickListener() {&nbsp; @Override&nbsp; public void onClick(View v) {&nbsp; &nbsp; StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>(){&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public void onResponse(String s) {&nbsp; &nbsp; &nbsp; &nbsp; if(s.equals("Logged In")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(Login.this, Home.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Login.this, "Incorrect Details", Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }, new Response.ErrorListener(){&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public void onErrorResponse(VolleyError volleyError) {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Login.this, "Some error occurred -> "+volleyError, Toast.LENGTH_LONG).show();;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }) {&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; protected Map<String, String> getParams() throws AuthFailureError {&nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> parameters = new HashMap<String, String>();&nbsp; &nbsp; &nbsp; &nbsp; parameters.put("email", emailBox.getText().toString());&nbsp; &nbsp; &nbsp; &nbsp; parameters.put("password", passwordBox.getText().toString());&nbsp; &nbsp; &nbsp; &nbsp; return parameters;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; RequestQueue rQueue = Volley.newRequestQueue(Login.this);&nbsp; &nbsp; rQueue.add(request);&nbsp; }});

红颜莎娜

您还需要在代码中请求权限。请参阅: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) {&nbsp; &nbsp; if (ContextCompat.checkSelfPermission(thisActivity,&nbsp; &nbsp; &nbsp; &nbsp; permission)&nbsp; &nbsp; &nbsp; &nbsp; != PackageManager.PERMISSION_GRANTED) {&nbsp; &nbsp; &nbsp; &nbsp; ActivityCompat.requestPermissions(thisActivity,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new String[]{permission},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; requestId);&nbsp; &nbsp; }}@Overridepublic void onRequestPermissionsResult(int requestCode,&nbsp; &nbsp; &nbsp; &nbsp; String permissions[], int[] grantResults) {&nbsp; &nbsp; switch (requestCode) {&nbsp; &nbsp; &nbsp; &nbsp; case PERMISSION_INTERNET: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (grantResults.length <= 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || grantResults[0] != PackageManager.PERMISSION_GRANTED) {requestPermission(Manifest.permission.INTERNET, PERMISSION_INTERNET);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; case PERMISSION_ACCESS_NETWORK_STATE: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (grantResults.length <= 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || grantResults[0] != PackageManager.PERMISSION_GRANTED) {requestPermission(Manifest.permission.ACCESS_NETWORK_STATE, PERMISSION_ACCESS_NETWORK_STATE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java