美文网首页
okHTTP使用

okHTTP使用

作者: 会武功的蚊子 | 来源:发表于2017-09-27 18:38 被阅读0次

    Examples
    GET A URL
    This program downloads a URL and print its contents as a string. Full source.
    OkHttpClient client = new OkHttpClient();String run(String url) throws IOException { Request request = new Request.Builder() .url(url) .build(); Response response = client.newCall(request).execute(); return response.body().string();}
    POST TO A SERVER
    This program posts data to a service. Full source.
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");OkHttpClient client = new OkHttpClient();String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); Response response = client.newCall(request).execute(); return response.body().string();}

    相关文章

      网友评论

          本文标题:okHTTP使用

          本文链接:https://www.haomeiwen.com/subject/qjtpextx.html