连接拒绝尝试在Android中执行HttpGet

时间:2014-05-30 10:37:01

标签: android http

我正在尝试做一个简单的HttpGet但它甚至没有进入App空间,因为Fiddler没有出现。

private final static String EMPLOYEE_SERVICE_URI = "http://localhost:82/EmployeeService_deploy/EmployeeInfo.svc/GetEmployee/?key=";  

        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(EMPLOYEE_SERVICE_URI + "11");

        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");

        //get the response
        try
        {
            HttpResponse response = client.execute(request);

我有一种感觉,这可能是因为我的工作有防火墙而且这是在端口82上。我曾经想过,因为它可能允许它,但可能没有。在清单中,我有以下权限

<

uses-permission android:name="android.permission.INTERNET" />       
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

有没有人有任何建议,这可能是什么以及我如何解决它?我无法访问我的工作代理设置,因此如果这是问题,我无法指定代理。有什么方法可以通过端口80路由它吗?

05-30 06:32:34.890: W/System.err(1617): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:82 refused
05-30 06:32:34.890: W/System.err(1617):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
05-30 06:32:34.920: W/System.err(1617):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-30 06:32:34.920: W/System.err(1617):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-30 06:32:34.940: W/System.err(1617):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-30 06:32:34.940: W/System.err(1617):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-30 06:32:34.950: W/System.err(1617):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-30 06:32:34.950: W/System.err(1617):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-30 06:32:34.950: W/System.err(1617):     at lakecrest.tayle.network.tasks.GetData.doInBackground(GetData.java:65)
05-30 06:32:34.950: W/System.err(1617):     at lakecrest.tayle.network.tasks.GetData.doInBackground(GetData.java:1)
05-30 06:32:34.950: W/System.err(1617):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-30 06:32:34.950: W/System.err(1617):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-30 06:32:34.950: W/System.err(1617):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-30 06:32:34.950: W/System.err(1617):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-30 06:32:34.950: W/System.err(1617):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-30 06:32:34.980: W/System.err(1617):     at java.lang.Thread.run(Thread.java:841)
05-30 06:32:34.980: W/System.err(1617): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 82): connect failed: ECONNREFUSED (Connection refused)
05-30 06:32:34.980: W/System.err(1617):     at libcore.io.IoBridge.connect(IoBridge.java:114)
05-30 06:32:34.980: W/System.err(1617):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-30 06:32:34.980: W/System.err(1617):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
05-30 06:32:34.980: W/System.err(1617):     at java.net.Socket.connect(Socket.java:843)
05-30 06:32:35.020: W/System.err(1617):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-30 06:32:35.020: W/System.err(1617):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
05-30 06:32:35.030: W/System.err(1617):     ... 14 more
05-30 06:32:35.040: W/System.err(1617): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
05-30 06:32:35.050: W/System.err(1617):     at libcore.io.Posix.connect(Native Method)
05-30 06:32:35.050: W/System.err(1617):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
05-30 06:32:35.080: W/System.err(1617):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-30 06:32:35.080: W/System.err(1617):     at libcore.io.IoBridge.connect(IoBridge.java:112)
05-30 06:32:35.090: W/System.err(1617):     ... 19 more

2 个答案:

答案 0 :(得分:0)

使用异步任务来使用HTTP调用。

private class HttpCalling extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
                    String resp;
        //HTTP calls here
        return resp;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

    }
}

答案 1 :(得分:0)

您的应用是在模拟器上还是在设备上运行?哪里跑小提琴?如果你想使用127.0.0.1那么它们都应该在同一设备上的一个emulatof或两者上。