-1

I'm using Android asyntask with Pycharm(python with flask) server and it will return an array. When i tried to connect with server it will show connection refused. i need the solution why it shows the error like connection refused.

Android code:

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
Dinesh Kumar
  • 483
  • 8
  • 24

1 Answers1

0

ECONNREFUSED is to mean a connection is attempted to a remote server,However the remote host is not answering back. This is much like knocking into someone's door and no one is replying back. There is no direct answer for your scnerio as many things can cause this along the way. Please have a look in this and this threads. They might give you some insight into the core issue, and from that you can start analyzing and debugging your code.

Community
  • 1
  • 1
Yohannes Gebremariam
  • 2,225
  • 3
  • 17
  • 23