这是我的代码。
我正在尝试检查具有特定ID的订阅者是否订阅了我的频道。
public class HubActions {
private boolean reply;
public boolean isConnected(String id, String chan) {
String subKey = "sub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String pubKey = "pub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
Pubnub pubnub = new Pubnub(pubKey, subKey);
Callback callback = new Callback() {
public void successCallback(String channel, Object response) {
System.out.println(response.toString());
try {
JSONObject json = new JSONObject(response.toString());
JSONArray uuids = new JSONArray();
uuids = json.getJSONArray("uuids");
for(int i=0;i<json.getInt("occupancy");i++) {
if(uuids.get(i).equals(id)) {
System.out.println("Yup!");
reply = true;
break;
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void errorCallback(String channel, PubnubError error) {
System.out.println(error.toString());
}
};
pubnub.hereNow(chan, callback);
return reply;
}
public static void main(String[] args) {
boolean b = new HubActions().isConnected("2", "2_sub");
System.out.println(b);
}
}
这是我的输出
错误 {&#34;占用&#34;:2&#34;服务&#34;:&#34;存在&#34;&#34;消息&#34;:&#34; OK&#34;&# 34;状态&#34;:200,&#34; uuids&#34;:[&#34; 2&#34;,&#34; 3bbe065c-d84a-4529-a641-26d05439e71d&#34;]}
烨!
我不明白为什么返回 false ,即使该函数明确指定 true 到布尔变量的值回复
据我所知,当我们创建pubnub实例时,2个线程被初始化。我认为可能是线程仍在处理,而函数返回变量 reply 的默认值
但我该如何解决这个问题呢?
答案 0 :(得分:0)
这是一个异步问题。您正在调用hereNow
,它会立即返回,然后输出reply
的值,同时异步调用successCallback并运行循环。
这就是为什么在显示 Yup 之前看到reply
的值显示的原因。这样做:
public class HubActions {
private boolean reply;
public boolean isConnected(String id, String chan) {
String subKey = "sub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String pubKey = "pub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
Pubnub pubnub = new Pubnub(pubKey, subKey);
Callback callback = new Callback() {
public void successCallback(String channel, Object response) {
System.out.println(response.toString());
try {
JSONObject json = new JSONObject(response.toString());
JSONArray uuids = new JSONArray();
uuids = json.getJSONArray("uuids");
for(int i=0;i<json.getInt("occupancy");i++) {
if(uuids.get(i).equals(id)) {
System.out.println("Yup!");
reply = true;
break;
}
}
// output value of reply here
// or implement your own listener for the
// caller to receive the update
System.out.println(reply);
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void errorCallback(String channel, PubnubError error) {
System.out.println(error.toString());
}
};
pubnub.hereNow(chan, callback);
return reply;
}
public static void main(String[] args) {
boolean b = new HubActions().isConnected("2", "2_sub");
// b will always be false due to async nature of code
// must implement a listener to get the result from this scope
// System.out.println(b);
}
}
在上面的代码中,我正在寻找真正的解决方案,即实现successCallback
代码的侦听器模式并调用以通知main
中的原始调用者。
我所做的只是评论了您的System.out.println(b)
,因为在for循环结束后false
始终为System.out.println(reply)
并在successCallback
内进行Axiom LEM: forall P:Prop, P \/ ~P.
Theorem farmer: forall A B C:Prop,
(B /\ C -> A) -> (B /\ ~C -> A) -> (B -> A).
intros.
destruct (LEM C); tauto.
Qed.
。