Android emulator cannot resolve internet address - android

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

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 FTP error - 425 Can't open data connection

I'm using this to upload some file. It works if I in a local connection, but if I use a external connection, i get this message: 425 Can't open data connection. from the ftp server.
I use the org.apache.commons.net.ftp.FTPClient and org.apache.commons.net.ftp.FTPFile libs.
public static String gravaImagem(String photoFile) {
FTPClient mFtp = new FTPClient();
try {
mFtp.connect(FTPHOST, PORTA);
mFtp.login(USUARIO, SENHA);
mFtp.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
mFtp.setFileType(FTPClient.BINARY_FILE_TYPE);
String origem = Environment.getExternalStorageDirectory().getPath()+File.separator+"Pictures"+File.separator+"ImageSec"+File.separator+photoFile;
FileInputStream fis = new FileInputStream(origem);
mFtp.storeFile(photoFile, fis);
mFtp.logout();
mFtp.disconnect();
} catch (SocketException e) {
e.printStackTrace();
return "Fail. (ERR#CON3)";
} catch (IOException e) {
e.printStackTrace();
return "Fail. (ERR#CON4)";
}
return "Imagem enviada ao servidor.";
}
Debug shows no exceptions.
From the internet:
First - the most common solution: change the active/passive mode
settings. But that might not work, and if it does its only a band-aid
covering up the real problem.
As I've mentioned in the past, one of the most common reasons that
this error occurs is a misconfiguration of the FTP server software
itself, related to SSL connections and firewalls, in which the
connection tries to establish itself on a bogus ip address. Read more
about FTP SSL through a NAT firewall here, some potential solutions
are included.
There are other less likely causes, such as:
The server is configured to always use the same port for passive mode connections, or the client is configured to always use the
same port for active mode connections, although in this case
usually the software in question should raise a different error
first, but I've seen this happen.
In passive mode, the firewall in front of the FTP server doesn't have the correct ports open. So the server tells the client to
connect to ipaddress 1.2.3.4 on port x, but the firewall doesn't
allow incoming connections on port x. Most firewalls are smart
enough to open up the port when it sees the PASV response. Vice
versa for active mode and the firewall in front of the FTP client.
From me:
I've used this library on andoird and it worked well, so see my copy/paste section.

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 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 httprequest java.net.UnknownHostException

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.

Categories

Resources