org.apache.http.conn.HttpHostConnectException:连接到http:// localhost在android中被拒绝

时间:2012-12-31 08:31:16

标签: android apache connection

我正在创建一个将图像上传到服务器的应用程序,并在执行它时将其数据库更新到android中的服务器,它显示错误

Connection to http://localhost refused 

还有更多错误,我研究了这个问题,发现不是提供URL连接

http://localhost/... provide the link as http://10.0.2.2/... 

我这样做但问题是一样的 更多错误在这里:

E/org.apache.http.conn.HttpHostConnectException(4318): Connection to http://localhost refused
E/org.apache.http.conn.HttpHostConnectException(4318): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
E/org.apache.http.conn.HttpHostConnectException(4318):  at com.example.android.photobyintent.ViewRecipients.uploadFile(ViewRecipients.java:325)
E/org.apache.http.conn.HttpHostConnectException(4318):  at com.example.android.photobyintent.ViewRecipients$1.run(ViewRecipients.java:238)
E/org.apache.http.conn.HttpHostConnectException(4318):  at java.lang.Thread.run(Thread.java:856)
E/org.apache.http.conn.HttpHostConnectException(4318): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
E/org.apache.http.conn.HttpHostConnectException(4318):  at libcore.io.IoBridge.connect(IoBridge.java:114)
E/org.apache.http.conn.HttpHostConnectException(4318):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
E/org.apache.http.conn.HttpHostConnectException(4318):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
E/org.apache.http.conn.HttpHostConnectException(4318):  at java.net.Socket.connect(Socket.java:842)
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
E/org.apache.http.conn.HttpHostConnectException(4318):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
E/org.apache.http.conn.HttpHostConnectException(4318):  ... 8 more
E/org.apache.http.conn.HttpHostConnectException(4318): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
E/org.apache.http.conn.HttpHostConnectException(4318):  at libcore.io.Posix.connect(Native Method)
E/org.apache.http.conn.HttpHostConnectException(4318):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
E/org.apache.http.conn.HttpHostConnectException(4318):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
E/org.apache.http.conn.HttpHostConnectException(4318):  at libcore.io.IoBridge.connect(IoBridge.java:112)
E/org.apache.http.conn.HttpHostConnectException(4318):  ... 13 more

文件上传的代码是:

public int uploadFile(ArrayList<String> sourceFileUri, String info, String latitude, String longitude, String id) throws IOException {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost("http://10.0.2.2/deliverysystem/order/add");

            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            entity.addPart("returnformat", new StringBody("json"));
            System.out.println(sourceFileUri.size());
            for(int i=0;i<sourceFileUri.size();i++){
                String sourceFile = sourceFileUri.get(i);
                entity.addPart("uploaded_file"+(i+1), new FileBody(new File(sourceFile)));
            }

            entity.addPart("fld_delivery_id", new StringBody(id));
            entity.addPart("fld_delivery_location", new StringBody(info));
            entity.addPart("fld_latitude", new StringBody(latitude));
            entity.addPart("fld_longitude", new StringBody(longitude));
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost, localContext);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            response.getEntity().getContent(), "UTF-8"));

            String sResponse = reader.readLine();
            return 1;
        } catch (Exception e) {
            if (dialog.isShowing())
                dialog.dismiss();

            Log.e(e.getClass().getName(), e.getMessage(), e);
            return 1;
        }

       }

调试代码时达到:

httpPost.setEntity(entity);

并显示此行之后的异常,即在此声明中:

HttpResponse response = httpClient.execute(httpPost, localContext);

有人可以帮我吗?

12 个答案:

答案 0 :(得分:23)

如果您使用模拟器为本地服务器运行您的应用程序。将本地ip提及为10.0.2.2,并且必须在您的应用中提供互联网权限:

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

答案 1 :(得分:13)

<uses-permission android:name="android.permission.INTERNET"/> 将此标记添加到Android项目的AndroidManifest.xml中 ,它会没事的。

答案 2 :(得分:7)

http://localhost

上面的主机已经被运行代码的模拟器占用。如果要访问计算机的本地主机,请使用IP地址http://10.0.2.2:8080/

有关详细信息,请参阅此link

答案 3 :(得分:4)

我遇到了完全相同的问题,我对我的网址进行了以下更改,之后它工作得很好..

自:

http://localhost/Image4android/get_all_images.php

要:

http://192.168.1.2/Image4android/get_all_images.php

where the ip: 192.168.1.2 is IPv4 address. In windows Run > CMD > Ipconfig

答案 4 :(得分:3)

除了上面提到的将localhost更改为10.0.2.2:8080的答案之外,尝试在清单的应用程序标记之外添加权限

答案 5 :(得分:3)

用于wamp服务器使用10.0.2.2用于本地主机 例如10.0.2.2/phpMyAdmin

并且对于tomcat使用10.0.2.2:8080/server

答案 6 :(得分:2)

请检查您是否在同一网络上运行Android设备。这将解决问题。玩得开心!!!

答案 7 :(得分:1)

我正在使用PHP进行webservice和Android 4.x.用于连接到Web服务的设备。我有类似的问题,使用10.0.2.2与模拟器配合良好但无法从设备连接。对我有用的解决方案是:查找计算机的IP ...说192.168.0.103找到apache的端口...说8080现在打开httpd.conf并找到以下行Listen {{1在此行之后添加以下Listen 127.0.0.1:8080就是这样。现在,如果您在Android代码中使用192.168.0.103:8080,它将连接!!

答案 8 :(得分:0)

我有类似的问题,然后我发现我的智能手机没有连接wifi。在打开wifi并连接到类似网络(使用我的笔记本电脑)之后,又出现了另一个问题 - 笔记本电脑的防火墙阻止了传入的连接。一旦我修复了防火墙,我就可以从我的Android应用程序与笔记本电脑上运行的Web服务进行通信。

答案 9 :(得分:0)

导致此错误的基本和简单的事情之一是: 无互联网连接

首先开启设备的互联网连接。

(可能我们会忘记这样做)

答案 10 :(得分:0)

此错误的两种解决方案:

1。 在Android项目的androidManifest.xml中添加此权限

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

2。 首先开启设备的互联网连接。

答案 11 :(得分:-3)

使用127.0.0.1代替localhost