Have run into a roadblock with this socket app. It all worked fine in
Java with Eclipse, but when we moved it over to Android, it won't run.
When run on Android emulator (both 1.5 and 2.1), we get "false(gave up
waiting on response)"
The part that needs explaining is that after the NMEA sentence is
sent, we should get a data stream until we close the socket.
1. Why do we get "false" in the text box? No clue on that one.
Hopefully it will give us a clue as to why it breaks.
2. And what might be different between straight Java and Android
sockets/programs or wrong with our code that we get this exception
instead of the data stream we get when just running as Java app?
See code below. I have XXXXed out sensitive info. Thanks for any
help you can give.
package com.XXXXXXXmobile;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import android.os.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class XXXXXXXmobile extends Activity {
/** Called when the activity is first created. */
// TODO: these will come from some sort of user-input box
private final String XXXXXCasterHost = "156.63.XXX.XXX";
private final int XXXXXCasterPort = 2101;
private final String XXXXXMountPoint = "ODOT_RTCM3";
private final String XXXXXUsername = "westXXXXX";
private final String XXXXXPassword = "melXXXXX";
private final String Base64Credentials =
"d2VXXXXuZmFyXXXXXXXXXXX";
private final String NEMASentence = "$GPGGA,184500.00,4014.753,N,
08316.243,W,4,08,1.0,271.0,M,-33.391,M,,*51";
private TextView textBox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textBox = (TextView) findViewById(R.id.TextView01);
XXXXConnection();
}
public void XXXXConnection()
{
// create the socket
Socket sock = new Socket();
BufferedReader sockIn = null;
PrintWriter sockOut = null;
// TODO: do some parameter checking on the host name and port
InetSocketAddress XXXXCasterAddr = new
InetSocketAddress(XXXXCasterHost, XXXXCasterPort);
try {
sock.connect(XXXXCasterAddr);
sockIn = new BufferedReader(new
InputStreamReader(sock.getInputStream()));
sockOut = new PrintWriter(sock.getOutputStream(), true);
}
catch(IOException e)
{
// TODO: display some nice error message to the user
d(e.getMessage());
}
try
{
// send it!
sockOut.println("GET /" + XXXXMountPoint + " HTTP/1.0");
sockOut.println("User-Agent: XXXX XXXXMobileAndroid/xxxxxxxx");
sockOut.println("Accept: */*");
sockOut.println("Connection: close");
if(Base64Credentials.length() > 0)
{
sockOut.println("Authorization: Basic " +
Base64Credentials);
}
sockOut.println();
waitForResponse(sockIn, 1);
String response = "";
// get the response (looking for "ICY 200 OK")
response = sockIn.readLine();
d(response);
if(response.contains("ICY 200 OK"))
{
// get the extra blank line
sockIn.readLine();
CollectData(sockIn, sockOut);
}
else
{
throw new IOException("Didn't get ICY 200 OK");
}
sockOut.close();
sockIn.close();
sock.close();
}
catch(Exception e)
{
// TODO: display some nice error message to the user
d(e.getMessage());
}
}
public void CollectData(BufferedReader sockIn, PrintWriter
sockOut) throws IOException, InterruptedException
{
sockOut.println(NEMASentence);
sockOut.println();
waitForResponse(sockIn, 2);
// get the correction data
int i = 0;
while(sockIn.ready() && i < 100000000)
{
d(sockIn.readLine());
i++;
}
}
public void waitForResponse(BufferedReader sockIn, int j) throws
IOException, InterruptedException
{
// hang out and wait for something back
for(int i=0; i < 300; i++)
{
if(!sockIn.ready())
{
Thread.sleep(100);
}
else
{
break;
}
}
if(!sockIn.ready())
{
throw new IOException("Gave up waiting for a response." + j);
}
}
public void d(String msg)
{
Boolean DEBUG = true;
if(DEBUG)
{
textBox.append(msg);
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en