套接字接收 null (Android)

发布于 2024-09-01 12:00:54 字数 1522 浏览 2 评论 0原文

我有一个与服务器通信的 Android 应用程序(用 java 编写)。 在这两部分之间我已经建立了一个套接字连接并想要发送数据。 我遇到的问题是,有时对于某些用户来说,到达服务器的信息为空。

这有效(适用于所有手机、所有用户):
服务器:

int a = Integer.parseInt(in.readLine());
int b = Integer.parseInt(in.readLine());
int c = Integer.parseInt(in.readLine());
int d = Integer.parseInt(in.readLine());
String checksum = in.readLine();
String model = in.readLine();
String device = in.readLine();
String name = in.readLine();

客户端:

out.println(a);
out.println(b);
out.println(c);
out.println(d);
out.println(hash);
out.println(Build.MODEL);
out.println(Build.DEVICE);
String name = fixName();
out.print(name);
out.flush();

这不起作用(对于某些用户):
服务器:

int a = Integer.parseInt(in.readLine());
String checksum = in.readLine();
String model = in.readLine();
String device = in.readLine();
String name = in.readLine();
String msg = in.readLine();
int version = -1;
String test = "hej";
try{
    test = in.readLine();
    version = Integer.parseInt(test);
}catch(Exception e){
    e.printStackTrace();
}

客户端:

out.println(a);
out.println(hash);
out.println(Build.MODEL);
out.println(Build.DEVICE);
String name = fixName();
if(name == null) name = "John Doe";
out.println(name);
String msg = fixMsg();
if(msg == null) name = "nada";
out.println(msg);
out.println(curversion);
out.flush();

有时,在第二种情况下,名称、消息和版本(字符串测试)在服务器端为空。由于 test 为 null,因此触发了 catch。
curversion,a 是整数,其余的是字符串。

有什么想法吗?

I have a android app that is communicating with a server (written in java).
Between these two parts I have established a Socket connection and want to send data.
The problem I am having is that sometimes, for some users, the information that reaches the server is null.

This works (for all phones, all users):
Server:

int a = Integer.parseInt(in.readLine());
int b = Integer.parseInt(in.readLine());
int c = Integer.parseInt(in.readLine());
int d = Integer.parseInt(in.readLine());
String checksum = in.readLine();
String model = in.readLine();
String device = in.readLine();
String name = in.readLine();

Client:

out.println(a);
out.println(b);
out.println(c);
out.println(d);
out.println(hash);
out.println(Build.MODEL);
out.println(Build.DEVICE);
String name = fixName();
out.print(name);
out.flush();

This does not work (for some users):
Server:

int a = Integer.parseInt(in.readLine());
String checksum = in.readLine();
String model = in.readLine();
String device = in.readLine();
String name = in.readLine();
String msg = in.readLine();
int version = -1;
String test = "hej";
try{
    test = in.readLine();
    version = Integer.parseInt(test);
}catch(Exception e){
    e.printStackTrace();
}

Client:

out.println(a);
out.println(hash);
out.println(Build.MODEL);
out.println(Build.DEVICE);
String name = fixName();
if(name == null) name = "John Doe";
out.println(name);
String msg = fixMsg();
if(msg == null) name = "nada";
out.println(msg);
out.println(curversion);
out.flush();

Sometimes, in the second case, the name, msg, and version (the string test) are null at the server side. The catch is triggered because test is null.
curversion,a are ints, the rest are strings.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽月 2024-09-08 12:00:54

这里的编码很差。每次调用 readLine() 时,您必须立即测试结果是否为 null,如果为 true,则停止读取并关闭流。

这种特殊情况就是扫描仪的工作。

Poor coding here. Every time you call readLine() you must immediately test the result for null, and if true stop reading and close the stream.

This particular situation is a job for Scanner.

极度宠爱 2024-09-08 12:00:54

错误在其他地方,错过了许可。不,这不是互联网,而是 READ_PHONE_STATE。当 READ_PHONE_STATE 不可用时,对于某些手机上的某些版本,getDeviceId() 返回 null。

The fault was somewhere else, missed a permission. No, it was not INTERNET, it was READ_PHONE_STATE. getDeviceId() returns null for some versions on some phones when READ_PHONE_STATE is not available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文