-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocketJSONClient.java
82 lines (67 loc) · 2.27 KB
/
SocketJSONClient.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package advmobdev.unipr.it.landmarkrecognition;
import android.content.Context;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.widget.Toast;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import static java.lang.Thread.sleep;
public class SocketJSONClient extends AsyncTask<Void, Void, String> {
String dstAddress;
int dstPort;
int response = 0;
JSONObject jsonObject = new JSONObject();
boolean success;
Context context;
SocketJSONClient(String addr, int port, JSONObject jsonObject, Context context) {
dstAddress = addr;
dstPort = port;
this.jsonObject = jsonObject;
this.context = context;
}
@Override
protected String doInBackground(Void... arg0) {
DataInputStream is;
DataOutputStream os;
try {
Socket socket = new Socket(dstAddress, dstPort);
// is = new DataInputStream(socket.getInputStream());
os = new DataOutputStream(socket.getOutputStream());
PrintWriter pw = new PrintWriter(os);
pw.println(jsonObject.toString());
System.out.println("Send to the socket jsonObject.");
pw.flush();
// BufferedReader in = new BufferedReader(new InputStreamReader(is));
// String response = in.readLine();
// System.out.println("Response: " + response);
// is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
if (success = true)
return "Success";
else
return "Failed";
}
@Override
protected void onPostExecute(String result) {
if (success) {
Toast.makeText(context, "Connection Done", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Unable to connect", Toast.LENGTH_SHORT).show();
}
}
}