Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.firstapp;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.os.SystemClock;
- import android.text.TextUtils;
- import android.view.Display;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.IOException;
- import okhttp3.Call;
- import okhttp3.Callback;
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.Response;
- public class MainActivity extends AppCompatActivity {
- TextView text;
- EditText input;
- EditText usernameInput;
- EditText serverIp;
- OkHttpClient okhttpClient = new OkHttpClient();
- int messageCount;
- JSONArray newMessages;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- messageCount = 0;
- text = (TextView)findViewById(R.id.text);
- //text.setText("Et cette fois, รงa fonctionne !\n");
- input = findViewById(R.id.input);
- usernameInput = findViewById(R.id.usernameInput);
- serverIp = findViewById(R.id.serverIp);
- Display display = getWindowManager().getDefaultDisplay();
- usernameInput.setWidth((int)display.getWidth());
- serverIp.setWidth((int)display.getWidth());
- input.setWidth((int)Math.floor(0.75 * display.getWidth()));
- //Button button = findViewById(R.id.button);
- //button.setOnClickListener(new View.OnClickListener() {OnClick(View v)});
- new Thread(
- new Runnable() {
- public void run() {
- while (true) {
- getNewMessage();
- SystemClock.sleep(1000);
- }
- }
- }).start();
- }
- public void OnClick(View v){
- if (TextUtils.isEmpty(input.getText().toString()))
- return;
- String req = "query={\"req\": \"send\", \"username\": \"" +
- (TextUtils.isEmpty(usernameInput.getText().toString()) ? "Anonyme" :
- usernameInput.getText().toString()) + "\", \"message\": \"" +
- input.getText() + "\"}";
- try{
- Request myGetRequest = new Request.Builder()
- .url("http://" + serverIp.getText().toString() + "?"+req)
- .build();
- okhttpClient.newCall(myGetRequest).enqueue(new Callback() {
- @Override
- public void onFailure(Call call, IOException e) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- connexionLost();
- messageCount = 0;
- }
- });
- }
- @Override
- public void onResponse(Call call, Response response) throws IOException {
- final String responseText = response.body().string();
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- input.setText("");
- }
- });
- }
- });
- }
- catch (Exception e) {
- connexionLost();
- }
- }
- public void getNewMessage()
- {
- String req = "query={\"req\": \"receive\", \"count\": " + messageCount + "}";
- try {
- Request myGetRequest = new Request.Builder()
- .url("http://" + serverIp.getText().toString() + "?"+req)
- .build();
- okhttpClient.newCall(myGetRequest).enqueue(new Callback() {
- @Override
- public void onFailure(Call call, IOException e) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- connexionLost();
- messageCount = 0;
- }
- });
- }
- @Override
- public void onResponse(Call call, Response response) throws IOException {
- final String responseText = response.body().string();
- try {
- JSONObject res = new JSONObject(responseText);
- int tempCount = Integer.parseInt(res.getString("newCount"));
- messageCount = messageCount > tempCount ? 0 : tempCount;
- newMessages = (JSONArray) res.get("messages");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- String tempMessages = "";
- for (int i=0; i < newMessages.length(); i++) {
- try {
- tempMessages += newMessages.getString(i) + "\n" ;
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- if (text.getText() == "Serveur off ou pas de connexion\n")
- text.setText("");
- if (messageCount != 0)
- text.setText(text.getText() + tempMessages);
- else
- text.setText("");
- }
- });
- }
- });
- }
- catch (Exception e) {
- connexionLost();
- }
- }
- public void connexionLost()
- {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- text.setText("Serveur off ou pas de connexion\n");
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement