0

I'm testing my Application that is running on android using android studio. but I encountered problem "Java.net.ConnectException:Connection refused" by using HttpURLConnection.getConnection().

I already tried these:

  • Check the server is running?
  • access url from browser

This is my AsyncTask class:

public class MyTask extends AsyncTask<String, String, String> {

        protected  void onPostExecute(String... params){
            resValue = params[0];
        }

        @Override
        protected String doInBackground(String... params) {
            URL url;
            StringBuffer response = new StringBuffer();

            System.out.println(params[0]);
            try {
                url = new URL(params[0]);
            } catch (MalformedURLException e) {
                throw new IllegalArgumentException("invalid url");
            }

            HttpURLConnection conn = null;
            try {
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(false);
                conn.setDoInput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod("GET");
                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

                // handle the response
                int status = conn.getResponseCode();
                if (status != 200) {
                    throw new IOException("Post failed with error code " + status);
                } else {
                    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    String inputLine;
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (conn != null) {
                    conn.disconnect();
                }

                String responseJSON = response.toString();
                System.out.print("This is doInBackground "+responseJSON);
                return responseJSON;
            }
        }

    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Jin-guk Park
  • 221
  • 1
  • 3
  • 12
  • I would suggest using a proper http client like Volley or Retrofit, so that you don't need an AsyncTask but it also isn't clear what url you're using – OneCricketeer Apr 22 '19 at 02:28
  • Possible duplicate of [java.net.ConnectException: Connection refused](https://stackoverflow.com/questions/6876266/java-net-connectexception-connection-refused) – Jack Apr 22 '19 at 02:30
  • the example is work for me but My app is not.. why?? – Jin-guk Park Apr 22 '19 at 05:19
  • maybe.... because of my server is localhost?...... other domain is just work – Jin-guk Park Apr 22 '19 at 05:48

1 Answers1

0

Post the url you are using, it might be the problem. You should be using 10.0.2.2 ip instead of 127.0.0.1, because it is a default loopback interface ip.

admly
  • 319
  • 2
  • 10