-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonClient.java
90 lines (81 loc) · 3.54 KB
/
JsonClient.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
83
84
85
86
87
88
89
90
package com.soygul.jsonclient;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* HTTP client with JSON content type by default.
*/
public class JsonClient {
private final Gson gson = new Gson();
public <T> T get(String uri, Class<T> dataType) {
try {
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
connection.connect();
int status = connection.getResponseCode();
switch (status) {
case 200:
case 201:
/*Bitmap bitmap = BitmapFactory.decodeStream(connection.getInputStream());
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap);*/
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
return gson.fromJson(response.toString(), dataType);
}
} catch (MalformedURLException ex) {
//Logger.getLogger(DebugServer.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
//Logger.getLogger(DebugServer.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public <T> T post(String uri, Object data, Class<T> dataType) {
try {
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
String postData = new Gson().toJson(data);
connection.connect();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
writer.write(postData);
writer.flush();
writer.close();
int status = connection.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
Gson gson = new Gson();
return gson.fromJson(response.toString(), dataType);
}
} catch (MalformedURLException ex) {
//Logger.getLogger(DebugServer.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
//Logger.getLogger(DebugServer.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}