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?
Related
We are developing some internal apps for mobile devices that are connected to internal wifis. We have some problems because we are only checking if the devices are connected using ConnectivityManager. But we need to check not only if there is connection, we need to check that the connection between the device and the server is working.
The problem is that ConnectivityManager tell us that the wifi is connected. But if the device is in an area with little coverage the app have errors trying to connect.
How can we easily check that the connection we have open against the server is still responding correctly? For example, one of the applications the connection is open against a SQL Server. Is there any way to check that we get to the server and it gives us an ok, and that we are not losing the connection and the packages because of the low coverage?
Thanks!!
You can try pinging the server if you receive a NullPointerException or IOException most likely there is no connection or connection timed out.
you can read more here an answer to similar question by syb0rg. Also remember to wrap this piece of code in an AsyncTask or a Thread to prevent your app from crashing.
try {
//replace URL with your domain
URL url = new URL("http://www.google.com");
HttpURLConnection urlConnect = (HttpURLConnection) url.openConnection();
urlConnect.setConnectTimeout(1000);
urlConnect.getContent();
System.out.println("Connection established.");
} catch (NullPointerException np) {
np.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
}
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));
}
I've written a small file transfer program for android using standard Java sockets. The program works fine except for the following case:
I connect two android devices A and B over WiFi tethering connection. Device A is sharing the connection (enabled as wireless hotspot). When I run java server on A and client on B, the program works okay but when I run the server on device B, it can't accept any socket binding request from A. It doesn't throw any exception. Seems like the request is not reaching the server! However, both the devices are connected (ping test is okay in both directions). Can't I run socket server on a device connected as hotspot client? I thought once the networking is setup correctly, the application would work in any direction.
Also, Wireshark traces reveal nothing. What am I missing here? Please help! Here are my code snippets:
Server side (waiting for client connection):
while (true) {
try {
socket = serversocket.accept();
Runnable connectionHandler = new ConnectionHandler(
socket, fileArray, filepathNameArray,
SenderActivity.this, userID, handler);
new Thread(connectionHandler).start();
userID = userID + 1;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I would appreciate any kind of help! Thanks in advance.
I'm newbie on Android and Java programming and I'm stuck.
I was able to create a TCP/IP communication client (Over LAN) which sends text messages on a windows based web server. The code is working quite well on my Samsung Galaxy S Advance mobile phone which is currently running on 2.3.6. However trying the apk file on two other devices running Android 4.0 and Android 4.1 the App is running but no message arrives on the PC (test are preformed on the same network).
The function I'm using for sending packets is the following:
public void sednit(String IP,String MSG) {
try {
// Socket s = new Socket ("192.168.128.102",39999);
Socket s = new Socket (ipaddress,39999);
//outgoing stream redirect to socket
OutputStream out = s.getOutputStream();
PrintWriter output = new PrintWriter (out);
output.println(MSG);
output.flush();
BufferedReader input = new BufferedReader (new InputStreamReader(s.getInputStream)));
//read line(s)
String st = input.readLine();
//Close connection
s.close();
} catch (Exception e) {
// Toast.makeText(getApplicationContext(), e.toString(),Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Unable to communicate with player", Toast.LENGTH_LONG).show();
}
What Am I doing wrong?
Is there any limitation applied on Android 4 and newer or I have messed up my manifest file?
Thank you.
You are most likely getting a NetworkOnMainThread exception. Since Android Honeycomb, you are required to perform network operations in separate threads to improve UI responsiveness. The easiest ways to do this are to use an AsyncTask to manage your network operation if it's short, or to extend java.io.Thread if the connection needs to be maintained.
As Tomislav says in comment use Asynctask to network communication.
Also catch (Exception e) { Is an extremely bad Idea.. Catch the exceptions you are counting on, so that others may be thrown and you can see them. We have no idea what so ever what is going wrong with you program when you are doing this...
So please either do e.printStackTrace(); or remove the try/catch so we can get your logcat and help you.
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.