package com.nbusy.app.data;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;

/**
 * HTTP client with JSON content type by default.
 */
public class JsonClient {
    public <T> T getJSON(String uri, java.lang.Class<T> classOfT) {
        try {
            URL url = new URL(uri);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            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).append("\n");
                    }
                    reader.close();
                    Gson gson = new Gson();
                    return gson.fromJson(response.toString(), classOfT);
            }

        } 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;
    }
}

@@ -80,32 +80,64 @@
 ufferedReader;%0D%0A
+import java.io.BufferedWriter;%0D%0A
 import java.io.I
@@ -176,32 +176,98 @@
 tStreamReader;%0D%0A
+import java.io.OutputStream;%0D%0Aimport java.io.OutputStreamWriter;%0D%0A
 import java.net.
@@ -468,24 +468,69 @@
 onClient %7B%0D%0A
+    private final Gson gson = new Gson();%0D%0A%0D%0A
     public %3C
@@ -537,20 +537,16 @@
 T%3E T get
-JSON
 (String 
@@ -554,18 +554,8 @@
 ri, 
-java.lang.
 Clas
@@ -559,24 +559,24 @@
 lass%3CT%3E 
-classOfT
+dataType
 ) %7B%0D%0A   
@@ -755,24 +755,98 @@
 od(%22GET%22);%0D%0A
+            connection.setRequestProperty(%22Accept%22, %22application/json%22);%0D%0A
             
@@ -1344,20 +1344,1688 @@
 ine)
-.append(%22%5Cn%22
+;%0D%0A                    %7D%0D%0A                    reader.close();%0D%0A                    return gson.fromJson(response.toString(), dataType);%0D%0A            %7D%0D%0A%0D%0A        %7D catch (MalformedURLException ex) %7B%0D%0A            //Logger.getLogger(DebugServer.class.getName()).log(Level.SEVERE, null, ex);%0D%0A        %7D catch (IOException ex) %7B%0D%0A            //Logger.getLogger(DebugServer.class.getName()).log(Level.SEVERE, null, ex);%0D%0A        %7D%0D%0A        return null;%0D%0A    %7D%0D%0A%0D%0A    public %3CT%3E T post(String uri, Object data, Class%3CT%3E dataType) %7B%0D%0A        try %7B%0D%0A            URL url = new URL(uri);%0D%0A            HttpURLConnection connection = (HttpURLConnection) url.openConnection();%0D%0A            connection.setRequestMethod(%22POST%22);%0D%0A            connection.setRequestProperty(%22Content-Type%22, %22application/json%22);%0D%0A            connection.setRequestProperty(%22Accept%22, %22application/json%22);%0D%0A            connection.setDoOutput(true);%0D%0A            String postData = new Gson().toJson(data);%0D%0A            connection.connect();%0D%0A            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));%0D%0A            writer.write(postData);%0D%0A            writer.flush();%0D%0A            writer.close();%0D%0A            int status = connection.getResponseCode();%0D%0A%0D%0A            switch (status) %7B%0D%0A                case 200:%0D%0A                case 201:%0D%0A                    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));%0D%0A                    StringBuilder response = new StringBuilder();%0D%0A                    String line;%0D%0A                    while ((line = reader.readLine()) != null) %7B%0D%0A                        response.append(line
 );%0D%0A
@@ -3195,16 +3195,16 @@
 (), 
-classOfT
+dataType
 );%0D%0A

add post json