questions
I recently had a problem writing a project like this: Request a paragraph of text information to the server through POST request, get the JSON data containing the information and then use JsonObject() to parse it, also get the value of the information, but in recycleView can not automatically display, must manually slide recycleView to display the information on the interface. The following code
CommitResult= HttpUtil.post(talkUrl, token, "application/json", params);
JSONObject object=new JSONObject(CommitResult);
String result1=object.getJSONObject("result").getJSONObject("response").getJSONArray("name").get(0).toString();
JSONObject object1=new JSONObject(result1);
message1=object1.getString("message");
tb_Content = new Tb_Content(message1, 0);
tbContentList.add(tb_Content);
recyclerViewAdapter.notifyDataSetChanged();
Copy the code
Problem analysis
First of all, eliminate the problem of obtaining data. Data can be obtained and displayed after I manually slide recycleView, so it is likely that there is a problem with notifyDataSetChanged() of my recycleViewAdapter. NotifyDataSetChanged () has been notifyDataSetChanged() since it returns the result of the request page. NotifyDataSetChanged () has been notifyDataSetChanged() since it returns the result of the request page.
Try to approach
The idea here is that to avoid waiting for the variable to receive the return result, a new handler is created as a way to receive the data and then go back to the main thread for page updates. The code is as follows:
Handler handler; . CommitResult= HttpUtil.post(talkUrl, token,"application/json", params);
JSONObject object=new JSONObject(CommitResult);
String result1=object.getJSONObject("result").getJSONObject("response").getJSONArray("name").get(0).toString();
JSONObject object1=new JSONObject(result1);
message1=object1.getString("message");
Message m = newMessage(); m.obj=robotSay; handler.sendMessage(m); .@Override
protected void onResume(a) {
super.onResume();
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
tb_chatContent = new Tb_ChatContent(msg.obj.toString(), 0); tbChatContentList.add(tb_chatContent); recyclerViewAdapter.notifyDataSetChanged(); }};Copy the code
After re-running the program, you can update it automatically instead of manually swiping the screen.
The problem summary
On second thought, I examined the code and found that it was actually a very simple problem. HttpUtil is a class that someone else has wrapped up for HTTP requests. HTTP requests are made in the child thread. Very few applications force network requests in the main thread, so everything from there is done in the child thread. Naturally, UI update cannot be implemented, but the Handler mechanism is introduced. After completing the request to the network part, the received Message is encapsulated by Message object and sent out. This Message will be added to the queue of MessageQueue to be processed. Looper (the manager of the MessageQueue) tries to pull messages from MessageQueue and distribute them back to the Handler’s handleMessage() method. Since the handler is created in the main thread, after receiving the message, we can implement interface updates and other operations in the main thread based on the contents of the message. So far, the problem is solved, if there is anything improper to write in the article, please big guys criticism.