connect failed: ECONNREFUSED - java

I am developing one app, and what I think I wanna do is receive data from server database in android. So I started to run some tutorials. I found one that is doing pretty much what I want.
But I am getting:
detailMessage "failed to connect to localhost/127.0.0.1 (port 8080): connect failed: ECONNREFUSED (Connection refused)" (id=830021648792)
Whole code of this tutorial is here link
Error occurs on :
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
I checked that:
firewall is disabled
ping is working
connection string is exactly the same as when I run the servlet from server admin interface - http://localhost:8080/Requestor/RQSRV
-the servlet is running
My configuration :
Server : Glass Fish 4.0
Android App - Developed in Eclipse
Servlet - Developed in NetBeans

To access your PC localhost from Android emulator, use 10.0.2.2 instead of 127.0.0.1. localhost or 127.0.0.1 refers to the emulated device itself, not the host the emulator is running on.
Reference: https://developer.android.com/studio/run/emulator-networking#networkaddresses
For Genymotion use: 10.0.3.2 instead of 10.0.2.2

if you are using genymotion then go to command prompt type ipconfig
-find for Ethernet adapter virtualBox host-only network:
-find for Ipv4 Address..............192.1**.**.*
copy
http://192.1..*/foldername/filename/filename.json or filename.php
to check if its working go to browser in emulator copy paste the url you should find the file which you are looking in your browser..
Sorry for English..
This worked for me hope this might help some one..
Thank u..

Related

Could not Connect User to Service in Android Studio

I am using Android Studio 2.3. When I am attaching my Moto G4 to debug an application, it ends up with the following error in Android Monitor.
6333-7189/? I/XMPPConnection: Preparing to connect user 1576857969013772288 to service: on host: and port: 0
03-25 11:06:18.221 6333-7189/? E/XMPPConnectionManager: Failed to connect user '1576857969013772288' to host ' on port 0: XMPPError connecting to :0.: remote-server-error(502) XMPPError connecting to :0.
-- caused by: java.net.ConnectException: Connection refused
After this error, no further logs are captured even in verbose mode.
Check below Links:
https://stackoverflow.com/a/6876306/3992798
I am having a similar problem.
I have an application running on Websphere 7 Dev environment
http://appl.company.com/appl/home.action
And also on my localhost system
http://localhost/appl/home.action
I am trying to access a URL like below
http://appl.company.com/SiteContent/a.html from the above application
It works perfectly fine on my local system.
But on the development environment, on this particular line :-
String stream = (new java.net.URL(url)).openStream();
I am getting a "java.net.ConnectException: Connection refused" exception
On googling i figured, such problems are either due to one of the following
The port is wrong (cannot be this option, as same url running perfectly from local system)
Firewall is stopping it.
The Android SDK, do you config the path?
try to it what open a cmd window,input 'adb devices',see result waht display a device of you Moto

Connection to LocalHost/10.0.2.2 from Android Emulator timed out

Although this question has been asked multiple times in StackOverflow and I went through many of them, still I couldn't resolve my issue or I am not able to find out the root cause of the issue. Hence, posting a new question.
Below are the list of links I went through --
How to connect to my http://localhost web server from Android Emulator in Eclipse
Accessing localhost:port from Android emulator
How can I access my localhost from my Android device?
how to connect localhost in android emulator?
Here goes my code --
protected Void doInBackground(Void... params)
{
try
{
HttpURLConnection connection = null;
URL url = new URL("http://10.0.2.2:8080/android_connect/user_registration.php");
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setConnectTimeout(720000000);
connection.setReadTimeout(72000000);
connection.connect();
OutputStreamWriter output = new OutputStreamWriter(connection.getOutputStream());
output.write(emp_details.toString());
output.flush();
output.close();
HttpResult = connection.getResponseCode();
connection.disconnect();
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
I am trying to connect Android with MySQL database using PHP through WAMP server. The PHP file (user_registration.php) is being saved in the below path --
C:\wamp\www\android_connect
Now after executing the code, I am getting an error like "java.net.SocketTimeoutException: failed to connect to /10.0.2.2 (port 8080) after 720000000ms: isConnected failed: ETIMEDOUT (Connection timed out)".
I went through the particular link in order to resolve this issue --
Android connection to localhost
But could not understand how it has been resolved! :)
Can you please help me in this case? Please note I am using Android Studio for my build.
Thanks in advance!
Making a connection from your Android to your Computer is working with 10.0.2.2 only on an Google Android Virtual Device. Android Virtual Devices are listening for 10.0.2.2 and forwarding all the requests to your computer.
Genymotion Android Virtual Devices are listening on 10.0.2.3 and forwarding those requests to your computer.
10.0.2.2 is not working with your real Android device. If you want to use it with your real device you have to set the IP of your computer, as it has been suggested by a previous answer.
I have figured out the reason why it was not working.
There were two issues --
The IP Address was not correct. So I changed the IP Address from 10.0.2.2 to the IPv4 address - which can be obtained on windows by typeing ipconfig in the command prompt and see link for linux.
Also the port number 8080 was not correct. I have set my own port number in httpd.conf file, like
##Listen 12.34.56.78:8383Listen 0.0.0.0:8383Listen [::0]:8383##, under Apache and I used the same.
After changing both and re-starting the WAMP server, it worked like a charm.
Why to go for localhost or any ip address to run on emulator or real device simply go for ngrok to convert the localhost as a global address with very convenient and simplest way.
Accepted answer to this question is really unclear so maybe this will help someone.
Just got the same problem and resolved it by executing "adb reverse tcp:8080 tcp:8080", although after I revoked forwarding, the host machine remained visible and all ports were working, so it is unclear how it works.
Make sure that
you have no proxy in wifi settings
if you have, then 10.0.2.2 is added to bypass without mask
that you didn't make a typo, sometimes people put 0 instead of 2 like this 10.0.0.2:8080
In my situation, I had this code running on my android device:
private static final String BACKEND_URL = "http://10.0.2.2:4242/"; // 10.0.2.2 is the Android emulator's alias to localhost
But my server code was running on my PC. So since my PC and my android device are "not the same device", that's why "http://10.0.2.2:4242/" didn't work. Solution was to change "http://10.0.2.2:4242/" to "https://<IP_OF_SERVER>:4242/"

JSOUP and Android Emulator - unable to resolve host

I have virtual host on my Windows 8 - http://test.localhost to my localhost on wamp and test site. If i use this address in browsers then all working well.
If i use this in jsoup and my android apps:
Document doc = Jsoup.connect("http://test.localhost").post();
Then I have warning:
java.net.UnknownHostException: Unable to resolve host "test.localhost": No address associated with hostname
How can I connect by jsoup with my localhost and Android emulator?
EDIT:
URLs from Internet, for example stackoverflow.com etc working fine. I would like connect emulator with my localhost.
Your Android emulator is a Virtual Machine which has a network stack that is separate from your Windows machine. If Android did have DNS set to resolve test.localhost, it would resolve to itself, not to your Windows host.
You need to use your Windows local IP in the URL. Something like:
Document doc = Jsoup.connect("http://192.168.0.1").post();
You can get your local IP with cmd -> ipconfig
You can check that the Android emulator VM can connect through the network to your host with adb shell ping <ip>

Java Socket connection refused with my Public IP

I have a client server architecture project in android. I cant connect with my public IP to server. I closed firewall, and did the port redirection for server. My friend can connect from outside to my server, but i can't, why? how can it be?..
Thanks..
Which OS are you running your client server code. If Windows, look for c:\Windows\System32\drivers\etc\hosts, if it's linux go the /etc/hosts/ file
Open the file with sudo privileges.
Format:
<IP> <HOSTNAME>.<DOMAIN> <ALIAS>
Example:
127.0.0.1 localhost.localdomain localhost
Add your IP here with domain name. Your Domain Name can be anything and try again. Also your question is a bit vague. Please add more details such as your os env and what exactly are you trying to achieve?

Android Emulator get port Number

The port for emulator 5554 is not working when I try to type the command: telnet localhost 5554
The error is:
Connecting To localhost...Could not open connection to the host, on port 5554:
Connect failed
How could it connect to port 5554 to let the android emulator sending socket, and the android emulator program on client server side give a message that does not work ( unfortunately client has stopped)
Check whether your port is open or not using this link.
http://www.yougetsignal.com/tools/open-ports/
If u have personal firewall try disabling that.

Categories