I am trying to create a connection using localhost but is giving connection refused

0
public static String getJsonSystem(String urlString){

    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;
    String retorno = "";

    try{
        URL url = new URL(urlString);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.connect();

        InputStream in = new BufferedInputStream(urlConnection.getInputStream());

        reader = new BufferedReader(new InputStreamReader(in));
        String line = "";
        StringBuffer buffer = new StringBuffer();

        while ((line = reader.readLine()) != null){
            buffer.append(line);
        }

        retorno = buffer.toString();


    } catch (Exception e){
        e.printStackTrace();
    }


    return retorno;
}

Here's how I call the connection:

String caminho = "http://192.168.0.11/system/pages/login";
HttpConnection conexão = new HttpConnection();
String resp = conexão.getJsonSystem(caminho);

AlertDialog.Builder alerta = new AlertDialog.Builder(this);
alerta.setMessage(resp);
alerta.show();
    
asked by anonymous 25.03.2017 / 17:00

1 answer

1

If you are using the AVD emulator you should use 10.0.2.2 to access your computer's localhost. As described in official documentation .

For the emulator Genymotion you should use 10.0.3.2 .

- Edit

Remember to keep the http protocol in the link, in your case:

  

" link "

    
25.03.2017 / 21:07