引起:android.system.ErrnoException:isConnected失败:ECONNREFUSED(拒绝连接)

时间:2017-03-12 08:37:04

标签: java android 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
            }

logcat的:

03-12 13:52:48.194 23394-23527/com.example.dinesh.eis W/System.err:     at libcore.io.IoBridge.isConnected(IoBridge.java:267)
03-12 13:52:48.194 23394-23527/com.example.dinesh.eis W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:191)
03-12 13:52:48.194 23394-23527/com.example.dinesh.eis W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:127)
03-12 13:52:48.194 23394-23527/com.example.dinesh.eis W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:188)
03-12 13:52:48.194 23394-23527/com.example.dinesh.eis W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:461)
03-12 13:52:48.194 23394-23527/com.example.dinesh.eis W/System.err:     at java.net.Socket.connect(Socket.java:918)
03-12 13:52:48.195 23394-23527/com.example.dinesh.eis W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)
03-12 13:52:48.195 23394-23527/com.example.dinesh.eis W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:152)
03-12 13:52:48.195 23394-23527/com.example.dinesh.eis W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:282)
03-12 13:52:48.195 23394-23527/com.example.dinesh.eis W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:216)
03-12 13:52:48.195 23394-23527/com.example.dinesh.eis W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:391)
03-12 13:52:48.195 23394-23527/com.example.dinesh.eis W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
03-12 13:52:48.195 23394-23527/com.example.dinesh.eis W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:217)
03-12 13:52:48.195 23394-23527/com.example.dinesh.eis W/System.err:     at com.example.dinesh.eis.LoginActivity$BackTask.doInBackground(LoginActivity.java:121)
03-12 13:52:48.195 23394-23527/com.example.dinesh.eis W/System.err:     at com.example.dinesh.eis.LoginActivity$BackTask.doInBackground(LoginActivity.java:101)
03-12 13:52:48.195 23394-23527/com.example.dinesh.eis W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
03-12 13:52:48.195 23394-23527/com.example.dinesh.eis W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-12 13:52:48.195 23394-23527/com.example.dinesh.eis W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-12 13:52:48.195 23394-23527/com.example.dinesh.eis W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-12 13:52:48.195 23394-23527/com.example.dinesh.eis W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-12 13:52:48.197 23394-23527/com.example.dinesh.eis W/System.err:     at java.lang.Thread.run(Thread.java:818)
03-12 13:52:48.197 23394-23527/com.example.dinesh.eis W/System.err: Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
03-12 13:52:48.198 23394-23527/com.example.dinesh.eis W/System.err:     at libcore.io.IoBridge.isConnected(IoBridge.java:252)
03-12 13:52:48.198 23394-23527/com.example.dinesh.eis W/System.err:     ... 20 more

1 个答案:

答案 0 :(得分:0)

ECONNREFUSED是指尝试连接到远程服务器,但远程主机没有回复。这就像撞到某人的门,没有人回复。你的scnerio没有直接的答案,因为许多事情可能会导致这种情况。请查看thisthis个主题。他们可能会让您深入了解核心问题,然后您可以开始分析和调试代码。