使用以下代码读取输入流时会发生错误。我使用以下代码:
public void run() {
try {
SocketAddress sockaddr = new InetSocketAddress(nServer, nPort);
nsocket = new Socket();
nsocket.connect(sockaddr, 10 * 1000);
if (nsocket.isConnected()) {
nsocket.setSoTimeout(20 * 1000);
nis = nsocket.getInputStream();
nos = nsocket.getOutputStream();
if (nProtocol.equals("ntripv1")) {
String requestmsg = "GET /" + nMountpoint + " HTTP/1.0\r\n";
requestmsg += "User-Agent: NTRIP LefebureNTRIPClient/20131124\r\n";
requestmsg += "Accept: */*\r\n" + "Connection: close\r\n";
if (nUsername.length() > 0) {
requestmsg += "Authorization: Basic " + ToBase64(nUsername + ":" + nPassword) +"\r\n";
}
requestmsg += "\r\n";
nos.write(requestmsg.getBytes());
} else {
}
byte[] buffer = new byte[4096];
int read = nis.read(buffer, 0, 4096);
while (read != -1) {
byte[] tempdata = new byte[read];
System.arraycopy(buffer, 0, tempdata, 0, read);
try {
dataMessenger.send(Message.obtain(null, MSG_NETWORK_GOT_DATA, tempdata));
} catch (RemoteException e2) {
}
read = nis.read(buffer, 0, 4096);
}
}
} catch (SocketTimeoutException ex) {
try {
dataMessenger.send(Message.obtain(null, MSG_NETWORK_TIMEOUT));
} catch (RemoteException e2) {
}
} catch (Exception e) {
LogMessage(e.getLocalizedMessage());
e.printStackTrace();
} finally {
try {
nis.close();
nos.close();
nsocket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
try {
dataMessenger.send(Message.obtain(null, MSG_NETWORK_FINISHED));
} catch (RemoteException e2) {
}
}
}
服务器端可以很好地与其他应用程序配合使用。此代码还与服务器建立连接并获取一些日期,但过了一会儿它在 nis.read 上相当。
代码是用java 1.8 for android studio编写的。
有没有人有想法从服务器获取所有信息并读取所有信息,以便可以在应用程序中使用。
更新 在while循环中使用以下行给出错误:
read = nis.read(buffer, 0, 4096);
更新 2 给定的错误是: System.err: java.net.SocketException: Connection reset