[{"data":1,"prerenderedAt":77},["Reactive",2],{"/samples/java/retrofit":3,"related:9aJfH4tuNh":59},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":8,"link":11,"body":12,"_type":54,"_id":55,"_source":56,"_file":57,"_extension":58},"/samples/java/retrofit","java",false,"","Retrofit","Retrofit is a type-safe HTTP client library for Java and Android. It simplifies the process of consuming RESTful web services by generating Java interface definitions based on RESTful API endpoints. Retrofit handles the boilerplate code for making HTTP requests and parsing responses, making it easy to work with web APIs in Java applications.","Java","https://square.github.io/retrofit/",{"type":13,"children":14,"toc":51},"root",[15,30,42],{"type":16,"tag":17,"props":18,"children":19},"element","p",{},[20,28],{"type":16,"tag":21,"props":22,"children":25},"a",{"href":11,"rel":23},[24],"nofollow",[26],{"type":27,"value":8},"text",{"type":27,"value":29}," is a type-safe HTTP client library for Java and Android. It simplifies the process of consuming RESTful web services by generating Java interface definitions based on RESTful API endpoints. Retrofit handles the boilerplate code for making HTTP requests and parsing responses, making it easy to work with web APIs in Java applications.",{"type":16,"tag":31,"props":32,"children":36},"pre",{"className":33,"code":35,"language":5,"meta":7},[34],"language-java","import okhttp3.MediaType;\nimport okhttp3.OkHttpClient;\nimport okhttp3.Request;\nimport okhttp3.RequestBody;\nimport okhttp3.Response;\n\nimport org.json.JSONObject;\n\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.nio.file.Files;\nimport java.nio.file.Paths;\n\npublic class PDFConverter {\n\n    private static final MediaType JSON\n            = MediaType.get(\"application/json; charset=utf-8\");\n\n    private static OkHttpClient client = new OkHttpClient();\n\n    static byte[] convert(String apiKey, JSONObject params, String endpoint) throws IOException {\n        if (!endpoint.equals(\"pdf\") && !endpoint.equals(\"png\") && !endpoint.equals(\"jpg\") && !endpoint.equals(\"webp\")) {\n            throw new IllegalArgumentException(\"Invalid endpoint\");\n        }\n\n        String url = \"https://api.pdfshift.io/v3/convert/\" + endpoint;\n\n        RequestBody body = RequestBody.create(params.toString(), JSON);\n        Request request = new Request.Builder()\n                .url(url)\n                .post(body)\n                .addHeader(\"X-API-Key\", apiKey)\n                .build();\n        Response response = client.newCall(request).execute();\n\n        if (!response.isSuccessful()) {\n            throw new IOException(\"Unexpected code \" + response);\n        }\n\n        if (params.has(\"filename\") || params.has(\"webhook\")) {\n            // Return in JSON in this case\n            return new Gson().fromJson(new String(response.body().bytes()), JsonObject.class).toString().getBytes();\n        }\n\n        // Returns the bytes\n        return response.body().bytes();\n    }\n}\n",[37],{"type":16,"tag":38,"props":39,"children":40},"code",{"__ignoreMap":7},[41],{"type":27,"value":35},{"type":16,"tag":31,"props":43,"children":46},{"className":44,"code":45,"language":5,"meta":7},[34],"public static void main(String[] args) throws IOException {\n    JSONObject params = new JSONObject();\n    params.put(\"source\", \"https://en.wikipedia.org/wiki/REST\");\n    byte[] binary = convert(\"sk_XXXXXXXXXXXXXX\", params, \"pdf\");\n    Files.write(Paths.get(\"result.pdf\"), binary);\n}\n",[47],{"type":16,"tag":38,"props":48,"children":49},{"__ignoreMap":7},[50],{"type":27,"value":45},{"title":7,"searchDepth":52,"depth":52,"links":53},2,[],"markdown","content:samples:java:retrofit.md","content","samples/java/retrofit.md","md",[60,64,68,72,76],{"_path":61,"language":10,"library":62,"_id":63},"/samples/java/httpclient","HttpClient","content:samples:java:httpclient.md",{"_path":65,"language":10,"library":66,"_id":67},"/samples/java/javanet","java.net","content:samples:java:javanet.md",{"_path":69,"language":10,"library":70,"_id":71},"/samples/java/netty","Netty","content:samples:java:netty.md",{"_path":73,"language":10,"library":74,"_id":75},"/samples/java/okhttp","OkHttp","content:samples:java:okhttp.md",{"_path":4,"language":10,"library":8,"_id":55},1785426823718]