Advertisement
Karrix

Java chat exemple

Aug 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.15 KB | None | 0 0
  1. package com.example.firstapp;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.Bundle;
  6. import android.os.SystemClock;
  7. import android.text.TextUtils;
  8. import android.view.Display;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12.  
  13. import org.json.JSONArray;
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16.  
  17. import java.io.IOException;
  18.  
  19. import okhttp3.Call;
  20. import okhttp3.Callback;
  21. import okhttp3.OkHttpClient;
  22. import okhttp3.Request;
  23. import okhttp3.Response;
  24.  
  25. public class MainActivity extends AppCompatActivity {
  26.     TextView text;
  27.     EditText input;
  28.     EditText usernameInput;
  29.     EditText serverIp;
  30.     OkHttpClient okhttpClient = new OkHttpClient();
  31.     int messageCount;
  32.     JSONArray newMessages;
  33.  
  34.     @Override
  35.     protected void onCreate(Bundle savedInstanceState) {
  36.         super.onCreate(savedInstanceState);
  37.         setContentView(R.layout.activity_main);
  38.         messageCount = 0;
  39.         text = (TextView)findViewById(R.id.text);
  40.         //text.setText("Et cette fois, รงa fonctionne !\n");
  41.         input = findViewById(R.id.input);
  42.         usernameInput = findViewById(R.id.usernameInput);
  43.         serverIp = findViewById(R.id.serverIp);
  44.         Display display = getWindowManager().getDefaultDisplay();
  45.         usernameInput.setWidth((int)display.getWidth());
  46.         serverIp.setWidth((int)display.getWidth());
  47.         input.setWidth((int)Math.floor(0.75 * display.getWidth()));
  48.         //Button button = findViewById(R.id.button);
  49.         //button.setOnClickListener(new View.OnClickListener() {OnClick(View v)});
  50.  
  51.         new Thread(
  52.                 new Runnable() {
  53.             public void run() {
  54.                 while (true) {
  55.                     getNewMessage();
  56.                     SystemClock.sleep(1000);
  57.                 }
  58.             }
  59.         }).start();
  60.     }
  61.  
  62.     public void OnClick(View v){
  63.         if (TextUtils.isEmpty(input.getText().toString()))
  64.             return;
  65.         String req = "query={\"req\": \"send\", \"username\": \"" +
  66.                 (TextUtils.isEmpty(usernameInput.getText().toString()) ? "Anonyme" :
  67.                         usernameInput.getText().toString()) + "\", \"message\": \"" +
  68.                 input.getText() + "\"}";
  69.         try{
  70.             Request myGetRequest = new Request.Builder()
  71.                     .url("http://" + serverIp.getText().toString() + "?"+req)
  72.                     .build();
  73.             okhttpClient.newCall(myGetRequest).enqueue(new Callback() {
  74.                 @Override
  75.                 public void onFailure(Call call, IOException e) {
  76.                     runOnUiThread(new Runnable() {
  77.                         @Override
  78.                         public void run() {
  79.                             connexionLost();
  80.                             messageCount = 0;
  81.                         }
  82.                     });
  83.                 }
  84.  
  85.                 @Override
  86.                 public void onResponse(Call call, Response response) throws IOException {
  87.                     final String responseText = response.body().string();
  88.                     runOnUiThread(new Runnable() {
  89.                         @Override
  90.                         public void run() {
  91.                             input.setText("");
  92.                         }
  93.                     });
  94.                 }
  95.             });
  96.         }
  97.         catch (Exception e) {
  98.             connexionLost();
  99.         }
  100.     }
  101.  
  102.     public void getNewMessage()
  103.     {
  104.         String req = "query={\"req\": \"receive\", \"count\": " + messageCount + "}";
  105.         try {
  106.             Request myGetRequest = new Request.Builder()
  107.                     .url("http://" + serverIp.getText().toString() + "?"+req)
  108.                     .build();
  109.             okhttpClient.newCall(myGetRequest).enqueue(new Callback() {
  110.                 @Override
  111.                 public void onFailure(Call call, IOException e) {
  112.                     runOnUiThread(new Runnable() {
  113.                         @Override
  114.                         public void run() {
  115.                             connexionLost();
  116.                             messageCount = 0;
  117.                         }
  118.                     });
  119.                 }
  120.  
  121.                 @Override
  122.                 public void onResponse(Call call, Response response) throws IOException {
  123.                     final String responseText = response.body().string();
  124.                     try {
  125.                         JSONObject res = new JSONObject(responseText);
  126.                         int tempCount = Integer.parseInt(res.getString("newCount"));
  127.                         messageCount = messageCount > tempCount ? 0 : tempCount;
  128.                         newMessages = (JSONArray) res.get("messages");
  129.                     } catch (JSONException e) {
  130.                         e.printStackTrace();
  131.                     }
  132.                     runOnUiThread(new Runnable() {
  133.  
  134.                         @Override
  135.                         public void run() {
  136.                             String tempMessages = "";
  137.                             for (int i=0; i < newMessages.length(); i++) {
  138.                                 try {
  139.                                     tempMessages += newMessages.getString(i) + "\n" ;
  140.                                 } catch (JSONException e) {
  141.                                     e.printStackTrace();
  142.                                 }
  143.                             }
  144.                             if (text.getText() == "Serveur off ou pas de connexion\n")
  145.                                 text.setText("");
  146.                             if (messageCount != 0)
  147.                                 text.setText(text.getText() + tempMessages);
  148.                             else
  149.                                 text.setText("");
  150.                         }
  151.                     });
  152.                 }
  153.             });
  154.         }
  155.         catch (Exception e) {
  156.             connexionLost();
  157.         }
  158.     }
  159.  
  160.     public void connexionLost()
  161.     {
  162.         runOnUiThread(new Runnable() {
  163.  
  164.             @Override
  165.             public void run() {
  166.                 text.setText("Serveur off ou pas de connexion\n");
  167.             }
  168.         });
  169.     }
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement