-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathServerCommunicationRunnable.java
66 lines (56 loc) · 2.59 KB
/
ServerCommunicationRunnable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.doing.more.java.example.sillyclient;
import android.os.Handler;
import android.widget.TextView;
import org.quickconnectfamily.json.JSONInputStream;
import org.quickconnectfamily.json.JSONOutputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
import java.util.HashMap;
public class ServerCommunicationRunnable implements Runnable {
private int numMessagesSent;
private String messageForServer;
private WeakReference responseViewReference;
public ServerCommunicationRunnable(int currentNumMessagesSent, String aMessageForServer, WeakReference aResponseViewReference) {
this.numMessagesSent = currentNumMessagesSent;
this.messageForServer = aMessageForServer;
this.responseViewReference = aResponseViewReference;
}
public void run() {
try {
URL url = new URL("http://10.0.2.2:8080/json");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);//allows POST
JSONOutputStream outToServer = new JSONOutputStream(connection.getOutputStream());
HashMap<String, Object> request = new HashMap<>();
request.put("command", "Speak");
request.put("message", messageForServer);
outToServer.writeObject(request);
JSONInputStream inFromServer = new JSONInputStream(connection.getInputStream());
HashMap<String, Object> response = (HashMap<String, Object>) inFromServer.readObject();
final TextView responseView = (TextView) responseViewReference.get();
responseView.post(new Runnable() {
@Override
public void run() {
if (responseView != null) {
responseView.setText("Sent " + ServerCommunicationRunnable.this.messageForServer
+ ". Message number " + ServerCommunicationRunnable.this.numMessagesSent);
}
}
});
} catch (Exception e) {
e.printStackTrace();
final String theErrorMessage = e.getLocalizedMessage();
final TextView responseView = (TextView) responseViewReference.get();
responseView.post(new Runnable() {
@Override
public void run() {
if (responseView != null) {
responseView.setText("Error: Unable to communicate with server. " + theErrorMessage);
}
}
});
}
}
}