okHTTP使用

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,306评论 19 139
  • 参考okhttp官方wiki https://github.com/square/okhttp/wiki/Call...
    WangGavin阅读 11,969评论 0 1
  • OkHttp使用完全教程 标签 : Http请求, 类库blog : http://blog.csdn.net/o...
    oncealong阅读 171,081评论 27 460
  • 参考Android网络请求心路历程Android Http接地气网络请求(HttpURLConnection) 一...
    合肥黑阅读 21,457评论 7 63
  • 外面雨下的很大,我好想在纱窗旁看着它,听着它,写着它。可是我没这福气,雨似乎不是专门为我下的,我回到住处,它就和我...
    迷石阅读 1,800评论 0 0