Advertisement
Guest User

Untitled

a guest
Aug 26th, 2017
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. public class MsgChatActivity extends AppCompatActivity {
  2.  
  3. Socket mSocket;
  4.  
  5. MsgHistoryAPIService msgHistoryAPIService;
  6. RecyclerView recyclerView;
  7. ChatConversationAdapter chatAdapter;
  8. List<Datum> data = new ArrayList<>();
  9.  
  10.  
  11. String frId_TV, owner_type_TV, subject_TV;
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_msg_chat);
  17.  
  18. // initialize Socket
  19. ChatApplication app = (ChatApplication) getApplication();
  20. mSocket = app.getSocket();
  21. mSocket.on(Socket.EVENT_CONNECT, onConnect);
  22. mSocket.on(username, onNewMessage);
  23. mSocket.connect();
  24.  
  25.  
  26. msgHistoryAPIService = RestClient.getClient().create(MsgHistoryAPIService.class);
  27.  
  28. recyclerView = (RecyclerView) findViewById(R.id.reyclerview_message_list);
  29. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  30.  
  31. chatAdapter = new ChatConversationAdapter(getApplicationContext(),data);
  32. recyclerView.setAdapter(chatAdapter);
  33.  
  34. frId_TV = "1";
  35. owner_type_TV = "1";
  36. subject_TV = "Message";
  37.  
  38. chatHistoryData();
  39.  
  40. }
  41.  
  42.  
  43.  
  44. private void chatHistoryData() {
  45. final MsgHistoryRequest msgHistoryRequest = new MsgHistoryRequest(
  46. frId_TV,
  47. owner_type_TV,
  48. subject_TV
  49. );
  50.  
  51. Call<MsgHistoryResponse> call = msgHistoryAPIService.chatHistory(token_key,
  52. msgHistoryRequest);
  53. call.enqueue(new Callback<MsgHistoryResponse>() {
  54. @Override
  55. public void onResponse(Call<MsgHistoryResponse> call, Response<MsgHistoryResponse> response) {
  56. Toast.makeText(MsgChatActivity.this, "" +response.body().getStatus() , Toast.LENGTH_LONG).show();
  57.  
  58. data.addAll(response.body().getData());
  59. Toast.makeText(MsgChatActivity.this, "" + data.size(), Toast.LENGTH_LONG).show();
  60. chatAdapter.notifyDataSetChanged();
  61. }
  62.  
  63. @Override
  64. public void onFailure(Call<MsgHistoryResponse> call, Throwable t) {
  65.  
  66. Toast.makeText(MsgChatActivity.this, "please try again" , Toast.LENGTH_LONG).show();
  67. }
  68. });
  69. }
  70.  
  71.  
  72. private Emitter.Listener onConnect = new Emitter.Listener(){
  73. @Override
  74. public void call(Object... args) {
  75.  
  76. }
  77. };
  78.  
  79.  
  80. public Emitter.Listener onNewMessage = new Emitter.Listener(){
  81.  
  82. @Override
  83. public void call(final Object... args) {
  84. MsgChatActivity.this.runOnUiThread(new Runnable(){
  85.  
  86. @Override
  87. public void run() {
  88. //args[i] is the data received
  89. JSONObject abc = (JSONObject) args[0];
  90.  
  91. Toast.makeText(MsgChatActivity.this, ""+ abc,
  92. Toast.LENGTH_LONG).show();
  93. }
  94. });
  95.  
  96. }
  97. };
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement