Yusuf Ali
Yusuf Ali

Reputation: 173

Everything is ok but I'm getting java.net.ConnectException

I actually posted too much question here about connecting a website. I'm sorry about that, guys.

Here is the my Json.java

public class Json extends Activity{

    TextView tvStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.json);  
        tvStatus = (TextView) findViewById(R.id.tvStatus);
        new showThat().execute("http://localhost");

    }

    public String readLocalhost() throws ClientProtocolException, IOException
    {
        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet("http://localhost");
        HttpResponse response = client.execute(get);
        StatusLine status = response.getStatusLine();
        String data = status.toString();    

        return data;
    }

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


        protected void onPreExecute(String f) {
            // TODO Auto-generated method stub
            f = "saffet";
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            try {
                String data = readLocalhost();
                return data;
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(String result) {
            tvStatus.setText(result);
        }

    }



}

I'm just trying to print status. Also here is the logcat!

10-23 14:13:53.276: D/gralloc_goldfish(1429): Emulator without GPU emulation detected.
10-23 14:13:54.057: W/System.err(1429): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
10-23 14:13:54.076: W/System.err(1429):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
10-23 14:13:54.076: W/System.err(1429):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-23 14:13:54.086: W/System.err(1429):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-23 14:13:54.086: W/System.err(1429):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-23 14:13:54.086: W/System.err(1429):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-23 14:13:54.086: W/System.err(1429):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-23 14:13:54.096: W/System.err(1429):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-23 14:13:54.096: W/System.err(1429):     at com.example.channel.Json.readLocalhost(Json.java:36)
10-23 14:13:54.096: W/System.err(1429):     at com.example.channel.Json$showThat.doInBackground(Json.java:55)
10-23 14:13:54.096: W/System.err(1429):     at com.example.channel.Json$showThat.doInBackground(Json.java:1)
10-23 14:13:54.096: W/System.err(1429):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-23 14:13:54.106: W/System.err(1429):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-23 14:13:54.106: W/System.err(1429):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-23 14:13:54.106: W/System.err(1429):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-23 14:13:54.106: W/System.err(1429):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-23 14:13:54.116: W/System.err(1429):     at java.lang.Thread.run(Thread.java:841)
10-23 14:13:54.116: W/System.err(1429): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
10-23 14:13:54.146: W/System.err(1429):     at libcore.io.IoBridge.connect(IoBridge.java:114)
10-23 14:13:54.146: W/System.err(1429):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-23 14:13:54.146: W/System.err(1429):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-23 14:13:54.146: W/System.err(1429):     at java.net.Socket.connect(Socket.java:842)
10-23 14:13:54.156: W/System.err(1429):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
10-23 14:13:54.156: W/System.err(1429):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
10-23 14:13:54.156: W/System.err(1429):     ... 15 more
10-23 14:13:54.166: W/System.err(1429): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
10-23 14:13:54.206: D/dalvikvm(1429): GC_FOR_ALLOC freed 134K, 8% free 2886K/3136K, paused 43ms, total 44ms
10-23 14:13:54.216: W/System.err(1429):     at libcore.io.Posix.connect(Native Method)
10-23 14:13:54.216: W/System.err(1429):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
10-23 14:13:54.216: W/System.err(1429):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
10-23 14:13:54.216: W/System.err(1429):     at libcore.io.IoBridge.connect(IoBridge.java:112)
10-23 14:13:54.216: W/System.err(1429):     ... 20 more

I'm not getting unfortunately stopped error. But I cannot print status.

EDIT: I use Xampp.

Upvotes: 0

Views: 2331

Answers (2)

josebama
josebama

Reputation: 838

if you try to connect to http://localhost you are trying to connect to a server inside the Android device, so I guess that what you want is to connect to a server running in your computer. Then what you have to do is connect to your computers local IP, so if your computers local IP is 192.168.1.1 the URL you provide for the connection would be http://192.168.1.1

Also bear in mind the port where your server is listening, usually the default configuration of the servers running in local is 8080 or 5000. So, if your server is listening to port 8080 and your computers IP is 192.168.1.1, the URL you should provide will be http://192.168.1.1:8080

Upvotes: 3

ZeusNet
ZeusNet

Reputation: 720

you can't access localhost from your android device(emulator) because there is no localhost on your device. You have to use the real ip adress of your PC to connect to your webserver. E.g. your ip adress is 192.168.127.1 you have to call

new showThat().execute("http://192.168.127.1");

Edit: Have a look at this thread

Cheers

Upvotes: 1

Related Questions