android HttpHostConnectException while trying to connect to a gwt page - java

i wrote a gwt project that works already. the only problem i have is, that i can not connect to the server with my self programmed android app. i can start my gwt page at http://127.0.0.1:8888 with the browser, but when i try to connect to a servlet that maps to http://127.0.0.1:8888/ip/ i get a HttpHostConnectException connection refused exception. i can access http://127.0.0.1:8888/ip/ easily with the browser easily but not with my android app. here is the android code where i try to connect to the server
try{
HttpClient httpclient = new DefaultHttpClient();
ArrayList<NameValuePair> postParameters;
httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://127.0.0.1:8888/ip/");
...
execute = httpclient.execute(httpPost);
catch (Exception e) {
Log.e(TAG, e.getMessage(), e); // connection refused
}
the doPost() method of that servlet (mapped to /ip/) never gets called when i try to post to it with my android app.
the same structure worked with my vaadin and jquery projects perfectly. just gwt is creating this problem.

the problem existed only locally (most probably because of the super develop mode). after i replaced the ip in the bindaddress in the intellij idea debug configuration from 127.0.0.1 to the device ip, the problem was solved

Related

Cannot make http GET request on localhost of the android device/emulator

I have made test application to investigate the issue related to http request. It is an android application where I'm making request to localhost:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://127.0.0.1")
.build();
try {
Response response = client.newCall(request).execute();
Log.v("krv", response.toString());
} catch (Exception e) {
Log.v("krv", e.toString());
}
As result on device emulator I have "java.net.ConnectException: Failed to connect to /127.0.0.1:80" exception. With any other request on an external resource(like google.com) all are Ok. Also I have installed IIS. Operating system is Windows 10.
P.S. Really what I need - to be able to make http request on stb device where http server is run(where I have the same problem). I guess both issues have similar reason. Could you give me any advice how to fix/debug my problem...
127.0.0.1 is the loopback address for localhost. But when you're inside an android OS, the localhost or 127.0.0.1 is android itself and that is not where your server is running.
If you want to connect to the host machine, you need to find out the IP address of your host machine (Windows 10) and use that IP address instead of 127.0.0.1.
See How to find your local IP address
However, If you want to refer to the computer which is running the Android emulator, use the IP address 10.0.2.2 instead.

Java HTTP Request on VPN only website

I am trying to build a website parser for one of our internal websites (accessible only from the company network - we get on the network through Cisco AnyConnect VPN).
I can access the site fine in any browser, but not using HTTP requests. Windows network and sharing center shows that I have two active networks:
The actual internet connection
The company network (without internet access).
Default HTTP client gets time out as I suppose it makes a request using the actual internet connection (and the website is not accessible to public), but using this code:
HttpParams params = httpClient.getParams();
params.setParameter(ConnRoutePNames.LOCAL_ADDRESS, InetAddress.getByName("10.x.x.x"));
I get the following error:
I/O exception caught when connectiong to /10.x.x.x -> {s} -> https://zzz.com:443: Network is unreachable: connect
Also, might be a stupid test but I have done a HTTP request to a "what is my ip" site and the IP is shown as my Wifi IP not the IP through VPN (which I get when I open browser and browse to a "what is my ip" website). Same thing (wrong IP) when I try this using a gui-less browser (Jaunt or HTMLUnit).
Please advise if any fixes for this.
ConnRoutePNames appears to be deprecated. See if the following works (I haven't tested):
HttpHost proxy = new HttpHost("10.x.x.x", 80);
HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CloseableHttpClient httpclient = HttpClients.custom()
.setRoutePlanner(routePlanner)
.build();

Not able to debug from one web app to another web app

I am running two web application using IntelliJ. Both are running on http port 8080 and for one I have changed jmx port to 9099. Issuse is when I try to call another app using httpget HttpGet httpGet = new HttpGet(http://localhost:8080/b-app); from the a-app, http request never hitting http://localhost:8080/b-app. Do I need any specific configuration in intelliJ for it?
Thanks for the help

Android web service call throws org.apache.http.NoHttpResponseException

WHAT I DID:
I have tried to call the web service by "POST" method from my android app using the following code.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://myserver.com:5060/convert/service");
try {
File file = new File(inputPath);
httppost.setHeader("Content-Type", "application/msword");
httppost.setHeader("Accept", "application/pdf");
httppost.setEntity(new ByteArrayEntity(readFile(file)));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (Exception e) {
e.printStackTrace();
}
When I do this api call, I am getting the following exception.
"org.apache.http.NoHttpResponseException: The target server failed to
respond"
But when I do the same web service call from standalone java project using the same code, I am getting the correct response.
Not getting any errors like above.
WHAT I WANT:
Based on my java web service call, I know that there is no issue in service and also the source code. So What may be the issue? How can I get rid of this issue?
I have fixed the issue.
The issue is because of port number 5060. I did found this by using TCP/IP monitor tool in eclipse.
By defaule 5060 port is not being called from android(I didn't know the reason). Because the request not hits the server.
So I have changed the port number to 8090 in my server and restarted the server.
Then I called the service from android. Now It's working great.

Accessing Servlet from android httpclient programs getting connection refused?

While I access the localhost servlet from android httpclient utils I am getting connection refused error. The servlet is running fine, but I am getting error.
This is the code:
String dmurl="http://127.0.0.1:9090/DataManagerProject/DMServlet";
HttpClient httpClient = new DefaultHttpClient();
HttpGet hrtreq = new HttpGet(dmurl);
HttpResponse resp = httpClient.execute(hrtreq);
String output = EntityUtils.toString(resp.getEntity());
I am getting following error org.apache.http.conn.httphostconnectexception connection to refused
if you are using emulator to run your app for local server. mention the local ip as 10.0.2.2 and have to give Internet permission into your app
For more details, please refer this link.
Also, Turn Off the firewall and any anti-virus application in your PC
Check whether you have internet access enabled in your emulator and in your application
<uses-permission android:name="android.permission.INTERNET"/>
To access internet data of the device or emulator from your application add the above line in your manifest xml file.Also check the servlet mapping is proper in your the servlet project.
Assuming your servlet runs on your computer the reason you can't get a connection is because your using the wrong IP. 127.0.0.1 is a loopback address meaning that if you connect to 127.0.0.1 on your Android device, it will try to connect to the Android device.
To make it work you have to find the IP address of the computer which runs the servlet and use that in the URL instead.
In addition make sure you have given the Internet permission to your app.

Categories

Resources