android httprequest java.net.UnknownHostException - android

I want to make a http request with android.
is use this:
void testHTTP()
{
HttpClient httpClient = new DefaultHttpClient();
HttpUriRequest request = new HttpPost("http://www.google.com");
try {
HttpResponse response = httpClient.execute(request);
//System.out.println("response protocol version: " + response.getProtocolVersion());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I have the internet permission set in my manifest file.
I keep getting java.net.UnknownHostException: www.google.com
My final purpose ios to be able to invoke a web service. So, I initially tried the ksoap2 library and the proper url and got the same result.
I suspect I have some bad setting either in my app or in my emulator.
can you give me some pointers on where to look?
it would also bee useful a working example of something similar. then i could check and if the problem persists i would know that is my development setup at fault.
btw, i am using mac snow leopard with eclipse and android 2.2 as a setup
thanks
regards

Check the emulator log messages. There's a good chance you just aren't asking for the INTERNET permission in your app manifest.

I've had problems with the emulator where the network icon shows that I have no service, and I can't connect to the Internet. Usually, restarting the emulator fixed it. Once, I had to create a new emulator device.
Do check for clues in the emulator/device logs. You can view them in Eclipse using the LogCat view in the DDMS perspective.

BenTobin's answer pointed me to something I was missing. I had restarted the emulator several times to no avail, but noticed the emulator's Wi-fi indicator was showing 0 bars. I went into settings, turned Wi-fi mode off and then on and also Airplane Mode On then off. This resolved it for me. My bars came back.

Delete you AVD recreate new AVD and restart the eclipse

If you have permission to access the Internet is at the manifest file and the WiFi is connected so can have problems sharing the internet from your computer via WiFi, Ethernet. Try restarting internet sharing on computer. Can then will be fixed.

Restart your wifi connection if you are testing on device, its work for me.

Related

SignalR android SDK only works on WiFi connection

To be clear, this question is referencing the Android SDK for SignalR...not SignalR running in a browser on an android device.
For some reason, I can not establish a connection to the server when using my wireless data network (3g). I'm able to connect to my server via web services and receive a JSON string of data, but SignalR will not connect.
SignalRFuture<Void> awaitConnection = connection.start();
try {
awaitConnection.get(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
// Handle ...
} catch (ExecutionException e) {
// Handle ...
} catch (TimeoutException e) {
Log.d(TAGSR,"it timed out");
e.printStackTrace();
}
this hits the timeout exception always when I'm NOT on WiFi...even if I bump it up to a minute +. What's weird is that as soon as it times out, the other aforementioned data loads instantly. If I turn my WiFi on my phone, this connects no problem at all.
Using the same SignalR connection with my browser, I'm able to connect via 3g with no problems at all...it's only the Android SDK version that pukes unless it's on WiFi. Is there some built in mechanism to the SDK that requires a certain connection speed? If so, is there a way to override that value? My 3g ain't a ferrari, but it's fast enough to work over the web version, and work fine at that....
any ideas?
TIA
no idea why, but it appears that it's the transport selection that's causing a failure on 3g.
when I comment out one or the other (serversentevents or longpolling) in AutomaticTransport, it works fine...
private void initialize(Logger logger) {
mTransports = new ArrayList<ClientTransport>();
mTransports.add(new ServerSentEventsTransport(logger));
//mTransports.add(new LongPollingTransport(logger));
}

android could not connect to ftp host

i am using the ftpclient library and followed their example to make a simple ftp client app, my problem is when trying to connect :
try {
mFTPClient = new FTPClient();
// connecting to the host
mFTPClient.connect(host, port);
}
catch (Exception e)
{
Log.d("", "Error: could not connect to host " + host);
}
i rechecked my ip and port and nothing wrong, i even downloaded an ftp app "ftpcafe" to check if the problem wasnt from my code it worked fine, * note that i added the
<uses-permission android:name="android.permission.INTERNET"/>
do i need more permissions? and what is going on? and one last thing when i used ftpcafe app it didnt prompt me to enter port number while its is needed in my code, can someone explain this as i am new to network programming thing?
Did you try without port (if port 21).
mFTPClient.connect(host);
You may have some information about the problem in the exception ?
Log.d("",e.getMessage());

Android - getNetworkInterface and needed permissions

got a strange problem here.
I have a function which reads the NetworkInterfaces and gets me the WiFi-Interface name.
This worked properly 2 weeks ago with the only permission "android.permission.ACCESS_NETWORK_STATE".
This week i mainly tested with 3G and did some minor changes to the overall code and today i noticed 'NetworkInterface.getNetworkInterfaces()' doesnt work anymore, giving me a "permission denied" exception. Few secs later my dear friend google tells me i need INTERNET permission...
-> I never ever used INTERNET permission in this project ???
So what could be wrong? Any way to get the NetworkInterfaces without using the internet permissions? It works again if i add it but idont really want to use it just for that.
Enumeration<NetworkInterface> interfaces = null;
try {
interfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}

Android Developing - Slow Internet Connection via 3G

I'm developing a Client/Server Application with Android/Rails and Communication via REST Json
When I'm testing my App via Emulator, all works fine.
Also when I deploy the app on my phone with WIFI on it works and the speed is ok.
But when I disconnect from WIFI and use the 3G connection, the phone becomes incredible Slow and it takes Minutes du get one JSON Response from the Server.
I'ved debug a little bit and the Problem seems to be the HTTPClient, which execute Method takes ulta long.
Other Apps with Network Access are working perfectly.
Any suggestions what to do?
HTC Magic
Android 1.6
My Http Connection Code:
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("URL");
try {
client.execute(get);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SOLVED: Some Problems with the Port. I changed my Server Port from 3000 to 80 and it works just fine. Thanks for the Comment, that was the great Hint :).
Thanks to Chris Stratton
Move to an area with better mobile data coverage?

Android emulator cannot resolve internet address

When I run this code from Java project it works fine.
InetAddress ia=null;
try {
ia = InetAddress.getByName("www.google.com");
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println(ia.getHostAddress());
When i try the same code from emulator (already tried few versions) I'm getting UnknownHostException. Internet works fine on emulator and I'm able to connect to IP address like "93.86.230.251" but never to hostname "something.org".
Please help
load a web page in the browser of your emulator if you can't load any page probably you´ll need a proxy configuration
https://developer.android.com/studio/run/emulator-networking.html#proxy

Categories

Resources