gpt4 book ai didi

java - 获取用户在线/离线状态

转载 作者:行者123 更新时间:2023-12-02 13:34:32 26 4
gpt4 key购买 nike

我有一个聊天应用程序,我需要开发用户在线状态,为此我每 15 秒在服务器上调用 API,这将返回所有登录的用户及其在线状态 0(离线)和 1(在线) 。

我需要在聊天时显示用户是在线还是离线(让应用程序未注销)。我有 1 个数组列表,其中显示应用程序启动时的所有登录用户 ID 及其详细信息,包括他们的在线状态和我创建的第二个 API 以 JSON 格式返回用户在线状态。我有以下选项来实现用户在线状态

  • 替换现有数组列表项:-我正在 JSON 中获取用户 ID 及其在线状态,但我需要每 15 秒运行一次循环,以替换现有数组列表中的值
  • 以某种方式存储 JSON,让我可以轻松找到用户 ID 及其状态:- 如果我将 JSON 存储在数组列表中,我需要运行循环来查找我不想要的 ID 及其状态,所以这是最好的存储方式JSON,这样我就可以通过用户 ID 轻松获取用户状态。

这是我获取 JSON 的方式

protected List<WrapperClass> doInBackground(Void... params) {
userSession=new UserSession(context,"Elaxer");
UserState_Update=new ArrayList<>();


String data = null;
try {
String ID=userSession.getUserID(); //Getting Value from shared pref
data = URLEncoder.encode("User_ID", "UTF-8") + "=" + URLEncoder.encode(ID, "UTF-8");
Log.d(TAG,"Login ID "+ ID);
Log.d(TAG,"DO IN BACKGROUND START ");
URL url=new URL(URL_Path_NearBy);
connection= (HttpURLConnection) url.openConnection();
Log.d(TAG,connection.toString());
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);

//For POST Only - Begin
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(data);
writer.flush();
writer.close();
os.close();
connection.connect();

InputStream inputStream=connection.getInputStream();
reader=new BufferedReader(new InputStreamReader(inputStream));
Log.d(TAG,"GET INPUT STREAM AND PUUTING INTO READER");
String line;
StringBuffer stringBuffer=new StringBuffer();
while ((line=reader.readLine())!=null){
stringBuffer.append(line);

}
String completeJSON=stringBuffer.toString();
Log.d(TAG,"JSON ARRAY START");

JSONObject parentArray=new JSONObject(completeJSON);
JSONArray jsonArray=parentArray.getJSONArray("uData");


String LastSeen;
int LoginStatus,User_ID;
int Rec_Online_Status;

for (int i = 0; i <jsonArray.length() ; i++) {
JSONObject childObject=jsonArray.getJSONObject(i);

LastSeen=childObject.getString("lastseen") ;
LoginStatus=childObject.getInt("login_status") ;
User_ID=childObject.getInt("User_ID");
String UseID= String.valueOf(User_ID);
Log.d(TAG,"JSON Values "+LastSeen+" "+LoginStatus+" "+User_ID);

WrapperClass wrapperClass=new WrapperClass(UseID,LoginStatus);
UserState_Update.add(wrapperClass);
}

return UserState_Update; //List<WrapperClass> UserState_Update

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}




return null;
}

这是 JSON 格式的响应

{
"status": "SUCCESS",
"uData": [
{
"User_ID": "4",
"login_status": "1",
"lastseen": "0000-00-00 00:00:00"
},
{
"User_ID": "1",
"login_status": "0",
"lastseen": "0000-00-00 00:00:00"
},
{
"User_ID": "12",
"login_status": "1",
"lastseen": "0000-00-00 00:00:00"
},
{
"User_ID": "33",
"login_status": "0",
"lastseen": "0000-00-00 00:00:00"
}
]
}

这是获取用户在线状态的正确方法吗? (我知道 FCM 是正确的方法,但 FCM 目前在应用服务器端尚未准备好)

更新2:-作为@XngPro推荐的代码,我在doinBackground上实现

Map<String,Online_Status_Wrapper.User> map=new HashMap<>();
Online_Status_Wrapper wrapper=gson.fromJson(completeJSON,Online_Status_Wrapper.class);
Log.d(TAG,"Wrapper Get Data value "+wrapper.getuData());
Log.d(TAG,"Wrapper Get Status value "+wrapper.getStatus());

for (Online_Status_Wrapper.User u: wrapper.getuData()){
map.put(u.getUser_ID(),u);
}


Log.d(TAG,"State of Other User users "+map.get(12).getLogin_status());//HERE I AM GETTING NullPointerException
return map;

最佳答案

我认为你应该使用 Java 序列化/反序列化库,如 Gson

希望能帮到你~

示例

private static void bar() {
String jsonStr = "{\"status\":\"SUCCESS\",\"uData\":[{\"User_ID\":\"4\",\"login_status\":\"1\",\"lastseen\":\"0000-00-00 00:00:00\"},{\"User_ID\":\"1\",\"login_status\":\"0\",\"lastseen\":\"0000-00-00 00:00:00\"},{\"User_ID\":\"12\",\"login_status\":\"1\",\"lastseen\":\"0000-00-00 00:00:00\"},{\"User_ID\":\"33\",\"login_status\":\"0\",\"lastseen\":\"0000-00-00 00:00:00\"}]}";
Gson gson = new Gson();
UserFoo userFoo = gson.fromJson(jsonStr, UserFoo.class);
Map<String, UserFoo.User> map = new HashMap<>();
for (UserFoo.User u : userFoo.getUData()) {
map.put(u.getUser_ID(), u);
}
System.out.println("userId: 12, loginState: " + map.get("12").getLogin_status());
}

public static class UserFoo {

/**
* status : SUCCESS
* uData : [{"User_ID":"4","login_status":"1","lastseen":"0000-00-00 00:00:00"},{"User_ID":"1","login_status":"0","lastseen":"0000-00-00 00:00:00"},{"User_ID":"12","login_status":"1","lastseen":"0000-00-00 00:00:00"},{"User_ID":"33","login_status":"0","lastseen":"0000-00-00 00:00:00"}]
*/

private String status;
private List<User> uData;

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public List<User> getUData() {
return uData;
}

public void setUData(List<User> uData) {
this.uData = uData;
}

public static class User {
/**
* User_ID : 4
* login_status : 1
* lastseen : 0000-00-00 00:00:00
*/

private String User_ID;
private String login_status;
private String lastseen;

public String getUser_ID() {
return User_ID;
}

public void setUser_ID(String User_ID) {
this.User_ID = User_ID;
}

public String getLogin_status() {
return login_status;
}

public void setLogin_status(String login_status) {
this.login_status = login_status;
}

public String getLastseen() {
return lastseen;
}

public void setLastseen(String lastseen) {
this.lastseen = lastseen;
}
}
}

关于java - 获取用户在线/离线状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43073708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com