Repository navigation
Expand file tree
/
Copy pathFriendListActivity.java
More file actions
155 lines (113 loc) · 4.69 KB
/
Copy pathFriendListActivity.java
File metadata and controls
155 lines (113 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.example.wmchat2026;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class FriendListActivity extends Activity {
ListView lvFriends;
TextView txtTitle;
// ✅调试输出框
TextView txtDebug;
Handler handler = new Handler();
List<HashMap<String, String>> friendList = new ArrayList<>();
SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friendlist);
txtTitle = findViewById(R.id.txtTitle);
txtTitle.setText("好友列表");
// ✅绑定调试框
txtDebug = findViewById(R.id.txtDebug);
txtDebug.setText("正在加载好友列表...");
lvFriends = findViewById(R.id.lvFriends);
adapter = new SimpleAdapter(
this,
friendList,
R.layout.item_friend,
new String[]{"username", "unread"},
new int[]{R.id.txtFriendName, R.id.txtUnread}
);
lvFriends.setAdapter(adapter);
lvFriends.setOnItemClickListener((parent, view, position, id) -> {
String friendName = friendList.get(position).get("username");
Intent it = new Intent(FriendListActivity.this, ChatActivity.class);
it.putExtra("friend", friendName);
startActivity(it);
});
// 加载好友列表
loadFriends();
}
// ✅调试输出函数(可以复制)
private void debug(String msg) {
handler.post(() -> txtDebug.setText(msg));
}
private void loadFriends() {
new Thread(() -> {
debug("开始请求好友列表...\nToken=" + Session.token);
try {
JSONObject res = ApiClient.post(
"http://wm.51ezu.com/wechat.php?action=list_friends",
"token=" + Session.token
);
debug("好友接口返回:\n\n" + res.toString(2));
if (res.getInt("code") != 200) {
debug("加载失败:" + res.optString("msg"));
return;
}
Object obj = res.getJSONObject("data").get("friends");
List<HashMap<String, String>> temp = new ArrayList<>();
// ✅情况1:JSONArray
if (obj instanceof JSONArray) {
JSONArray arr = (JSONArray) obj;
for (int i = 0; i < arr.length(); i++) {
String fname = arr.getString(i);
HashMap<String, String> map = new HashMap<>();
map.put("username", fname);
map.put("unread", "");
temp.add(map);
}
}
// ✅情况2:JSONObject
else if (obj instanceof JSONObject) {
JSONObject o = (JSONObject) obj;
JSONArray keys = o.names();
if (keys != null) {
for (int i = 0; i < keys.length(); i++) {
String key = keys.getString(i);
// 跳过 requests / friends 等字段
if (!key.matches("\\d+")) continue;
String fname = o.getString(key);
HashMap<String, String> map = new HashMap<>();
map.put("username", fname);
map.put("unread", "");
temp.add(map);
}
}
}
else {
debug("friends 数据格式异常:" + obj.toString());
return;
}
handler.post(() -> {
friendList.clear();
friendList.addAll(temp);
adapter.notifyDataSetChanged();
});
debug("好友加载成功,共 " + temp.size() + " 人");
} catch (Exception e) {
debug("发生异常:\n" + e.getMessage());
}
}).start();
}
}