So I’ve scoured the net (mostly SO, google) to find good examples of socket programming with Android. I’ve done lots of Android dev (just not with sockets). I dont understand why my readLine() ALWAYS RETURNS NULL. And please ignore the hideous code, this is a quick and dirty prototype for a friend. My overall goal is to establish a connection to a server, send header data (GET /MOUNTPOINT Content-Type: … etc…), and receive a response, based on the response I need to either continue the stream or close it. Here is my most recent attempts code:
String userPass = new String(Base64.encodeToString("user:password".getBytes(), Base64.DEFAULT));
boolean connected = false;
String requestmsg = "GET /MOUNTPOINT" + " HTTP/1.0\r\n";
requestmsg += "User-Agent: MYCUSTOMAGENT\r\n";
requestmsg += "Accept: */*\r\n";
requestmsg += "Connection: close\r\n";
requestmsg += "Authorization: Basic " + userPass;
requestmsg += "\r\n";
DataOutputStream dos = null;
DataInputStream dis = null;
try {
Log.d("ClientActivity", "Connecting...");
Socket socket = new Socket("1.2.3.4", 9000);
connected = true;
String data = "";
while (connected) {
try {
Log.d("ClientActivity", "C: Sending command.");
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
dos.write(requestmsg.getBytes());
Log.i("ClientActivity", "RequestMsg Sent");
StringBuilder sb = new StringBuilder();
while ((data = dis.readLine()) != null)
{
sb.append(data);
}
Log.i("ClientActivity", "C: Sent.");
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
I’ve tried using the HttpClient API but it inserts a “GET /” message in the header and I need to specify “GET /MOUNTPOINT”. Plus I don’t know if HttpClient allows streaming. Ive also tried URLConnection, but also it inserts either GET or POST by default. Thanks for any help!
Also I have tried the following:
SocketAddress sockaddr = new InetSocketAddress("1.2.3.4", 9000);
nsocket = new Socket();
nsocket.connect(sockaddr, 10 * 1000); // 10 second connection timeout
if (nsocket.isConnected()) {
//nsocket.setSoTimeout(20 * 1000); // 20 second timeout once data is flowing
nis = nsocket.getInputStream();
nos = nsocket.getOutputStream();
Log.i(NTAG, "Socket created, streams assigned");
// Build request message
requestmsg = "GET /MOUNTPOINT" + " HTTP/1.0\r\n";
requestmsg += "User-Agent: MYANDROIDAGENT\r\n";
requestmsg += "Accept: */*\r\n";
requestmsg += "Connection: close\r\n";
requestmsg += "Authorization: Basic " + userPass;
requestmsg += "\r\n";
nos.write(requestmsg.getBytes());
nos.write(genGPGGA().getBytes());
Log.i(NTAG, "Waiting for inital data...");
byte[] buffer = new byte[4096];
int read = nis.read(buffer); // This is blocking
//Log.i("ReadNTRIP", tempdata.toString());
String test = "TESTING";
while (read != -1) {
tempdata = new byte[read];
System.arraycopy(buffer, 0, tempdata, 0, read);
read = nis.read(buffer, 0, 4096); // This is blocking
}
}
} catch (SocketTimeoutException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
nis.close();
nos.close();
nsocket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Log.i(NTAG, "Finished");
}
But read always returns -1. Im ready to pull my hair out.
EDIT: I added a bounty (worth very little) – I don’t have much rep to give, but this problem has become a show stopper for me. To get the bounty, write a simple android program to connect to thor.lsu.edu, port 9000, send a simple GET / HTTP/1.0 command and actually get back data (using sockets – cannot be with URLConnection or HttpClient). Post the program and a screenshot of the results. I’ve tried it so many different ways I don’t know what else to do (using InputStreams, BufferedInputStreams, InputStreamReaders, etc…). I’ve even used an example Android program (THAT WORKS), yet I still don’t get back data in my implementation. I don’t know if its a timing issue or what.
Thanks
Edit: I’ve noticed that I cannot even browse to http://thor.lsu.edu:9000 in Androids browser. It gives a data connectivity problem. Any idea why this port may be blocked?
All I did was a few modifications to your code. I think the major change was just not using the DataInputStream since the documentation explicitly says to not use readLine() anymore on this data structure. http://download.oracle.com/javase/1.4.2/docs/api/java/io/DataInputStream.html#readLine%28%29
I also remove the authorization stuff since it wasn’t required. If you run this code somewhere, you will notice the log printing out the HTTP response.