28

I am making an application that uploads images to server and updates its database to server in android while executing it, it shows an error

Connection to http://localhost refused 

and many more errors, I researched this problem and found instead of providing the URL connection

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

and I did so but the problem is same more error is here:

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

the code for file upload is:

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;
        }

       }

while debugging the code reaches up to:

httpPost.setEntity(entity);

and it shows exception after this line ie on this statement:

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

Could any one help me on this?

kamaci
  • 72,915
  • 69
  • 228
  • 366
ugene
  • 551
  • 1
  • 7
  • 19

13 Answers13

23

if you are using emulator to run your app for local server. mention the local ipas 10.0.2.2 and have to give Internet permission into your app :

<uses-permission  android:name="android.permission.INTERNET" />
zheek
  • 742
  • 1
  • 11
  • 22
13

<uses-permission android:name="android.permission.INTERNET"/> add this tag above into AndroidManifest.xml of your Android project ,and it will be ok.

Jeffery Yuan
  • 149
  • 1
  • 4
7
http://localhost

The above host is already occupied by the emulator in which you are running the code. If you want to access the local host of your computer than use the IP Address as http://10.0.2.2:8080/.

For more details, please refer this link.

TNR
  • 5,839
  • 3
  • 33
  • 62
  • i have my link like this: `HttpPost httpPost = new HttpPost("http://10.0.2.2/deliverysystem/order/add")`; – ugene Dec 31 '12 at 08:37
  • sorry it throws error `E/org.apache.http.conn.HttpHostConnectException(4474): Connection to http://10.0.2.2:8080 refused` – ugene Dec 31 '12 at 08:44
  • and also i have the user permission for internet and even the file and databases are uploaded. and i need the response from the server where i cannot reach – ugene Dec 31 '12 at 08:48
4

i was facing exactly the same issue and i made following changes to my URL after which it was working perfectly fine..

From:

http://localhost/Image4android/get_all_images.php

To:

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
min2bro
  • 4,509
  • 5
  • 29
  • 55
3

try adding the permission outside the application tag of the manifest in addition to the above mentioned answers of changing localhost to 10.0.2.2:8080

Gridtestmail
  • 1,459
  • 9
  • 10
3

for wamp server use 10.0.2.2 for local host e.g. 10.0.2.2/phpMyAdmin

and for tomcat use 10.0.2.2:8080/server

zheek
  • 742
  • 1
  • 11
  • 22
Ali Nawab
  • 31
  • 2
2

Please check that you are running the android device over same network. This will solve the problem. have fun!!!

2

When you test with device you want to add your PC ip address.

in pc run in cmd Ipconfig

in ubuntu run terminal ifconfig

Then use "http://your_pc_ip_address:8080/register" insted of using "http://10.0.2.2:8080/register"

in my pc = 192.168.1.3

and also add internet permission to Manifest

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
hash
  • 5,336
  • 7
  • 36
  • 59
1

I am using PHP for webservice and Android 4.x. device for connecting to the webservice. I had similar problem where, using 10.0.2.2 worked well with emulator but failed to connect from device. The solution that worked for me is: Find IP of your computer ... say 192.168.0.103 Find the port of your apache ... say 8080 Now open httpd.conf and locate following line Listen 127.0.0.1:8080 After this line add following Listen 192.168.0.103:8080 Thats it. Now if you use 192.168.0.103:8080 in your android code, it will connect!!

zheek
  • 742
  • 1
  • 11
  • 22
0

I had the similar issue, then I found out that wifi was not connected in my smartphone. After I turned on the wifi and connected to the similar network (with my laptop), there was another issue - laptop's firewall was blocking incoming connections. Once I fixed the firewall, I was able to communicate from my android app with the web service running on the laptop.

Farkhod
  • 196
  • 4
  • 8
0

One of the basic and simple thing which leads to this error is: No Internet Connection

Turn on the Internet Connection of your device first.

(May be we'll forget to do so)

Prabs
  • 4,923
  • 6
  • 38
  • 59
0

Two solutions for this error:

1. add this permission in your androidManifest.xml of your Android project

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

2. Turn on the Internet Connection of your device first.

-3

use 127.0.0.1 instead of localhost

Jahh
  • 25
  • 3
  • Has sense if you take in consideration that the genymotion run it in virtual machine, not embedded in your pc. But should be the ip, like `10.0.1.13` – jose920405 Jun 14 '16 at 14:48