Преглед на файлове

get_ids ad get_messages

Lauren Green преди 6 години
родител
ревизия
ff7424bf5d
променени са 2 файла, в които са добавени 24 реда и са изтрити 17 реда
  1. 12
    9
      Client/src/main/java/SimpleShell.java
  2. 12
    8
      Client/src/main/java/YouAreEll.java

+ 12
- 9
Client/src/main/java/SimpleShell.java Целия файл

1
+import com.fasterxml.jackson.databind.ObjectMapper;
1
 import com.mashape.unirest.http.HttpResponse;
2
 import com.mashape.unirest.http.HttpResponse;
2
 import com.mashape.unirest.http.JsonNode;
3
 import com.mashape.unirest.http.JsonNode;
3
 import com.mashape.unirest.http.Unirest;
4
 import com.mashape.unirest.http.Unirest;
13
 public class SimpleShell {
14
 public class SimpleShell {
14
 
15
 
15
 
16
 
16
-    public static void prettyPrint(String output) {
17
-        // yep, make an effort to format things nicely, eh?
18
-        System.out.println(output);
17
+    public static void prettyPrint(JsonNode jsonNode) {
18
+        try {
19
+            ObjectMapper mapper = new ObjectMapper();
20
+            Object json = mapper.readValue(jsonNode.toString(), Object.class);
21
+            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
22
+        } catch (Exception e) {
23
+            System.out.println("Sorry, pretty print didn't work");
24
+        }
19
     }
25
     }
26
+
20
     public static void main(String[] args) throws java.io.IOException {
27
     public static void main(String[] args) throws java.io.IOException {
21
 
28
 
22
         YouAreEll webber = new YouAreEll();
29
         YouAreEll webber = new YouAreEll();
65
 
72
 
66
                 // ids
73
                 // ids
67
                 if (list.contains("ids")) {
74
                 if (list.contains("ids")) {
68
-                    String results = webber.get_ids();
69
-                    SimpleShell.prettyPrint(results);
75
+                    webber.get_ids();
70
                     continue;
76
                     continue;
71
                 }
77
                 }
72
 
78
 
73
                 // messages
79
                 // messages
74
                 if (list.contains("messages")) {
80
                 if (list.contains("messages")) {
75
-                    String results = webber.get_messages();
76
-                    SimpleShell.prettyPrint(results);
81
+                    webber.get_messages();
77
                     continue;
82
                     continue;
78
                 }
83
                 }
79
                 // you need to add a bunch more.
84
                 // you need to add a bunch more.
111
             //catch ioexception, output appropriate message, resume waiting for input
116
             //catch ioexception, output appropriate message, resume waiting for input
112
             catch (IOException e) {
117
             catch (IOException e) {
113
                 System.out.println("Input Error, Please try again!");
118
                 System.out.println("Input Error, Please try again!");
114
-            } catch (UnirestException e) {
115
-                e.printStackTrace();
116
             }
119
             }
117
             // So what, do you suppose, is the meaning of this comment?
120
             // So what, do you suppose, is the meaning of this comment?
118
             /** The steps are:
121
             /** The steps are:

+ 12
- 8
Client/src/main/java/YouAreEll.java Целия файл

2
 import com.mashape.unirest.http.JsonNode;
2
 import com.mashape.unirest.http.JsonNode;
3
 import com.mashape.unirest.http.Unirest;
3
 import com.mashape.unirest.http.Unirest;
4
 import com.mashape.unirest.http.exceptions.UnirestException;
4
 import com.mashape.unirest.http.exceptions.UnirestException;
5
+import org.json.JSONArray;
6
+import org.json.JSONObject;
5
 
7
 
6
 import java.io.IOException;
8
 import java.io.IOException;
7
 import java.net.HttpURLConnection;
9
 import java.net.HttpURLConnection;
22
 //        System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
24
 //        System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
23
     }
25
     }
24
 
26
 
25
-    public String get_ids(){
27
+    public void get_ids(){
26
 
28
 
27
         try {
29
         try {
28
             HttpResponse<JsonNode> jsonResponse = Unirest.get(underARock + "/ids")
30
             HttpResponse<JsonNode> jsonResponse = Unirest.get(underARock + "/ids")
29
                     .header("accept", "application/json")
31
                     .header("accept", "application/json")
30
                     .asJson();
32
                     .asJson();
31
 
33
 
32
-            String ids = jsonResponse.getBody().toString();
33
-            System.out.println(ids);
34
-            return ids;
34
+            SimpleShell.prettyPrint(jsonResponse.getBody());
35
+
35
         } catch (UnirestException e) {
36
         } catch (UnirestException e) {
36
             System.out.println("Error");
37
             System.out.println("Error");
37
         }
38
         }
38
 
39
 
39
-        return null;
40
     }
40
     }
41
 
41
 
42
-    public String get_messages() throws UnirestException {
42
+    public void get_messages() {
43
         try {
43
         try {
44
-        return MakeURLCall("/messages", "GET", "");
44
+            HttpResponse<JsonNode> jsonResponse = Unirest.get(underARock + "/messages")
45
+                    .header("accept", "application/json")
46
+                    .asJson();
47
+
48
+            SimpleShell.prettyPrint(jsonResponse.getBody());
49
+
45
         } catch (UnirestException e) {
50
         } catch (UnirestException e) {
46
             System.out.println("Error");
51
             System.out.println("Error");
47
         }
52
         }
48
-        return null;
49
     }
53
     }
50
 
54
 
51
     public String MakeURLCall(String mainurl, String method, String jpayload) throws UnirestException {
55
     public String MakeURLCall(String mainurl, String method, String jpayload) throws UnirestException {