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阅读 121,188评论 16 134
  • OkHttp使用完全教程 标签 : Http请求, 类库blog : http://blog.csdn.net/o...
    oncealong阅读 140,377评论 26 438
  • 参考Android网络请求心路历程Android Http接地气网络请求(HttpURLConnection) 一...
    合肥黑阅读 17,618评论 7 62
  • OkHttp是一个开源的Java的http网络请求库,常用于安卓的APP的网络请求。 第一步当然要导入jar包,由...
    liut_2016阅读 1,074评论 0 1
  • 外面雨下的很大,我好想在纱窗旁看着它,听着它,写着它。可是我没这福气,雨似乎不是专门为我下的,我回到住处,它就和我...
    迷石阅读 82评论 0 0