-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSimpleSocket.java
executable file
·60 lines (53 loc) · 2.34 KB
/
SimpleSocket.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
public class SimpleSocket {
int numMessagesSent = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the UI items to interact with
final TextView responseView =
(TextView)this.findViewById(R.id.response);
final EditText textInput =
(EditText)this.findViewById(R.id.message);
try{
//connect to the server
Socket toServer = new Socket("10.0.2.2", 9292);
//setup the JSON streams to be used later.
final JSONInputStream inFromServer =
new JSONInputStream(toServer.getInputStream());
final JSONOutputStream outToServer =
new JSONOutputStream(toServer.getOutputStream());
//add the on click listener to the button
Button sendButton =
(Button)this.findViewById(R.id.sendButton);
sendButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
numMessagesSent++;
//prepare the bean
ArrayList aDataList = new ArrayList();
aDataList.add(numMessagesSent);
aDataList.add(textInput.getText().toString());
CommunicationBean aBean =
new CommunicationBean();
aBean.setCommand("Speak");
aBean.setData(aDataList);
try {
//send the bean
outToServer.writeObject(aBean);
HashMap aMap =
(HashMap)inFromServer.readObject();
responseView.setText(aMap.toString());
} catch (JSONException e) {
e.printStackTrace();
responseView.setText("Error: Unable to trade beans with server.");
}
}
});
}
catch(Exception e){
e.printStackTrace();
responseView.setText("Error: Unable to communicate with server. "+e.getLocalizedMessage());
}
}
}