Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MsgChatActivity extends AppCompatActivity {
- Socket mSocket;
- MsgHistoryAPIService msgHistoryAPIService;
- RecyclerView recyclerView;
- ChatConversationAdapter chatAdapter;
- List<Datum> data = new ArrayList<>();
- String frId_TV, owner_type_TV, subject_TV;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_msg_chat);
- // initialize Socket
- ChatApplication app = (ChatApplication) getApplication();
- mSocket = app.getSocket();
- mSocket.on(Socket.EVENT_CONNECT, onConnect);
- mSocket.on(username, onNewMessage);
- mSocket.connect();
- msgHistoryAPIService = RestClient.getClient().create(MsgHistoryAPIService.class);
- recyclerView = (RecyclerView) findViewById(R.id.reyclerview_message_list);
- recyclerView.setLayoutManager(new LinearLayoutManager(this));
- chatAdapter = new ChatConversationAdapter(getApplicationContext(),data);
- recyclerView.setAdapter(chatAdapter);
- frId_TV = "1";
- owner_type_TV = "1";
- subject_TV = "Message";
- chatHistoryData();
- }
- private void chatHistoryData() {
- final MsgHistoryRequest msgHistoryRequest = new MsgHistoryRequest(
- frId_TV,
- owner_type_TV,
- subject_TV
- );
- Call<MsgHistoryResponse> call = msgHistoryAPIService.chatHistory(token_key,
- msgHistoryRequest);
- call.enqueue(new Callback<MsgHistoryResponse>() {
- @Override
- public void onResponse(Call<MsgHistoryResponse> call, Response<MsgHistoryResponse> response) {
- Toast.makeText(MsgChatActivity.this, "" +response.body().getStatus() , Toast.LENGTH_LONG).show();
- data.addAll(response.body().getData());
- Toast.makeText(MsgChatActivity.this, "" + data.size(), Toast.LENGTH_LONG).show();
- chatAdapter.notifyDataSetChanged();
- }
- @Override
- public void onFailure(Call<MsgHistoryResponse> call, Throwable t) {
- Toast.makeText(MsgChatActivity.this, "please try again" , Toast.LENGTH_LONG).show();
- }
- });
- }
- private Emitter.Listener onConnect = new Emitter.Listener(){
- @Override
- public void call(Object... args) {
- }
- };
- public Emitter.Listener onNewMessage = new Emitter.Listener(){
- @Override
- public void call(final Object... args) {
- MsgChatActivity.this.runOnUiThread(new Runnable(){
- @Override
- public void run() {
- //args[i] is the data received
- JSONObject abc = (JSONObject) args[0];
- Toast.makeText(MsgChatActivity.this, ""+ abc,
- Toast.LENGTH_LONG).show();
- }
- });
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement