原因:android.system.errno异常:isConnected失败:econnrefered(连接被拒绝)

2024-06-16 08:56:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Android asyntask和Pycharm(python with flask)服务器,它将返回一个数组。当我尝试与服务器连接时,它将显示连接被拒绝。 我需要一个解决方案,为什么它显示错误,如连接拒绝。在

Android代码:

public static String ip = "http://192.168.43.221:3000/";
    private class BackTask extends AsyncTask<String, String, String> {

            private String result = null,name,batch,id,dept,dob,phno,add,credits;

            @Override
            protected String doInBackground(String... params) {
                try {
                    String data = URLEncoder.encode("device", "UTF-8") + "=" +
                            URLEncoder.encode(device, "UTF-8") + "&" +
                            URLEncoder.encode("ID", "UTF-8") + "=" +
                            URLEncoder.encode(regno1, "UTF-8") + "&" +
                            URLEncoder.encode("Password", "UTF-8") + "=" +
                            URLEncoder.encode(password1, "UTF-8");

                    BufferedReader reader = null;
                    Log.d("checking", data);
                    try {
                        URL url = new URL(ip + "login/");
                        URLConnection con = url.openConnection();
                        con.setDoOutput(true);
                        OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
                        writer.write(data);
                        writer.flush();
                        //getting response back
                        reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                        Toast.makeText(getBaseContext(),reader.readLine(),Toast.LENGTH_LONG).show();
                        StringBuilder s = new StringBuilder();
                        String line = null;
                        while ((line = reader.readLine()) != null) {
                            s.append(line + "\n");
                        }
                        result = s.toString();
                    } catch (Exception e) {
    //                    Toast.makeText(getBaseContext(),"Connection error",Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    Toast.makeText(getBaseContext(),"Connection error",Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
                return result;

            }

            @Override
            protected void onPostExecute(String result) {
                Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
                SharedPreferences.Editor e = sp1.edit();
               if(!sp1.equals(null))
                {

                    e.putString("uname", regno1);
                    e.putString("pass", password1);
                    e.apply();
                }
                //    progress.dismiss();
                //notification.setText(result);
                if (result != null) {
                    Toast.makeText(getBaseContext(), result, Toast.LENGTH_SHORT).show();
                    result = result.trim();
                    name= setDetails(result);


                    if (name.equals("success")) {
                        Intent intent1 = new Intent(LoginActivity.this, HrActivity.class);
                        //intent.putExtra("json",result);
                        startActivity(intent);
                    } else {
                        errormsg.setText("*Check id / password");
                    }
                }
            }

            @Override
            protected void onPreExecute() {
                progress.show();
            }

            @Override
            protected void onProgressUpdate(String... text) {
                // progress. For example updating ProgessDialog
            }

对数猫:

^{pr2}$

Tags: newstringshowresultnulllengthutfreader
1条回答
网友
1楼 · 发布于 2024-06-16 08:56:26

econnrefered是指试图连接到远程服务器,但是远程主机没有响应。这很像是敲别人的门,却没有人回应。你的斯内里奥没有直接的答案,因为很多事情都会导致这一过程。请查看this和{a2}线程。它们可能会让您对核心问题有一些了解,并从中开始分析和调试代码。在

相关问题 更多 >