Zapis, odczyt API i plików - rlip/java GitHub Wiki

Zapis do pliku

        List<Map<String, String>> reportData2 = new ArrayList<>();
        for(Map<String, Object> reportDataRowMap : reportData){
            Map<String, String> newMap = new LinkedCaseInsensitiveMap<>();

            for(String key : reportDataRowMap.keySet()) {
                Object o = reportDataRowMap.get(key);
                String value =  o == null ? null : o.toString();
                newMap.put(key, value);
            }
            reportData2.add(newMap);
        }

        FileOutputStream fos = new FileOutputStream("t.tmp");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(reportData2);
        oos.close();

Wysyłanie danych

public class App {

    static final Proxy proxyTest = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.4", 3128));

    public static void main(String[] args) throws Exception {
        OkHttpClient.Builder builder = new OkHttpClient.Builder().proxy(proxyTest);
        OkHttpClient client = builder.build();

        RequestBody body = new FormBody.Builder()
                .add("id", "1")
                .add("title", "foo")
                .add("body", "bar")
                .add("userId", "1")
                .build();

        Headers headers = new Headers.Builder().add("Content-type", "application/json; charset=UTF-8").build();
        Request request = new Request.Builder()
                .url("https://jsonplaceholder.typicode.com/posts/1")
                .headers(headers)
                .method("PUT", body)
                .build();

        Response response = client.newCall(request).execute();
        System.out.println(response.body().string());
    }
}

Pobieranie danych

public class App {

    static final Gson gson = new Gson();
    static final Type type = new TypeToken<List<Cloth>>() {
    }.getType();
    static final Proxy proxyTest = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.4", 3128));

    public static void main(String[] args) throws Exception {
        printJsonSyncByURL();
        printJsonSyncByOkHttpClient();
        printJsonAsyncByOkHttpClient();
    }

    private static void printJsonSyncByURL() throws IOException {
        URL url = new URL("https://next.json-generator.com/api/json/get/VyvzEVv0I");
        List<Cloth> clothList = gson.fromJson(new BufferedReader(new InputStreamReader(url.openConnection(proxyTest).getInputStream())), type);
        clothList.stream().forEach(System.out::println);
    }

    private static void printJsonSyncByOkHttpClient() throws IOException {
        OkHttpClient.Builder builder = new OkHttpClient.Builder().proxy(proxyTest);
        OkHttpClient client = builder.build();
        Request request = new Request.Builder()
                .url("https://next.json-generator.com/api/json/get/VyvzEVv0I") //json
                .build();
        Response response = client.newCall(request).execute();
        String data = response.body().string();
        List<Cloth> clothList = gson.fromJson(data, type);
        clothList.stream().forEach(System.out::println);
    }

    private static void printJsonAsyncByOkHttpClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder().proxy(proxyTest);
        OkHttpClient client = builder.build();
        Request request = new Request.Builder()
//                .url("https://next.json-generator.com/api/json/get/VyvzEVv0I") //json
                .url("https://countwordsfree.com/download/xml/60c83afe-48e3-4786-9172-83fc66c96bc9") //xml
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();

                //json
//                Type type = new TypeToken<List<Cloth>>(){}.getType();
//                List<Cloth> clothList = gson.fromJson(data, type);
//                clothList.stream().forEach(System.out::println);

                // xml
                Serializer serializer = new Persister();
                try {
                    Cloth cloth = serializer.read(Cloth.class, data);
                    System.out.println(cloth);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(Call call, IOException e) {
                System.out.println("ERROR: " + toString());
            }
        });
    }
}
⚠️ **GitHub.com Fallback** ⚠️