课程咨询 :186 8716 1620      qq:2066486918

昆明Java培训 > java教程 > OKHttp的简单使用
  • OKHttp的简单使用

    发布:昆明Java培训      来源:博客园      时间:2016-11-09

  • 昆明达内Java培训的老师今天分享OKHttp的简单使用。

    后端Controller

    一个简单的spring mvc web应用。

    @RequestMapping(value = "/getUserList", produces = "application/json; charset=utf-8")

    @ResponseBody

    public String getUserList(int pageNo, int pageSize)

    {

    Map<String, Object> map = new HashMap<String, Object>();

    try

    {

    Map<String, Object> param = ew HashMap<String, Object>();

    param.put("pageNo", pageNo);

    param.put("pageSize", pageSize);

    List<User> userList = userService.queryAll(param);

    map.put("userList", userList);

    return gson.toJson(map);

    }

    catch (Exception e)

    {

    logger.error(e.toString(), e);

    }

    return gson.toJson(FAILD);

    }

    使用OkHttp发送Ge请求

    package cn.hdu.edu.okhttpdemo;

    import java.io.IOException;

    import com.squareup.okhttp.OkHttpClient;

    import com.squareup.okhttp.Request;

    import com.squareup.okhttp.Response;

    /**

    * Hello world!

    *

    */

    public class App

    {

    public String run(OkHttpClient client, String url) throws IOException {

    Request request = new Request.Builder().url(url).build();

    Response response = client.newCall(request).execute();

    return response.body().string();

    }

    public static void main( String[] args )

    {

    OkHttpClient client = new OkHttpClient();

    try

    {

    String res = new App().run(client, "http://localhost:8080/webbf/user/getUserList.do?pageNo=0&pageSize=10");

    System.out.println(res);

    }

    catch (IOException e)

    {

    e.printStackTrace();

    }

    }

    }

    使用OkHttp发送Post请求

    package cn.hdu.edu.okhttpdemo;

    import java.io.IOException;

    import com.squareup.okhttp.FormEncodingBuilder;

    import com.squareup.okhttp.MediaType;

    import com.squareup.okhttp.OkHttpClient;

    import com.squareup.okhttp.Request;

    import com.squareup.okhttp.Response;

    /**

    * Hello world!

    *

    */

    public class App

    {

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public String post(OkHttpClient client, String url) throws IOException

    {

    Request request = new Request.Builder()

    .url(url)

    .post(new FormEncodingBuilder()

    .add("pageNo", "0") //参数1

    .add("pageSize", "10") //参数二

    .build())

    .build();

    Response response = client.newCall(request).execute();

    return response.body().string();

    }

    public static void main(String[] args)

    {

    OkHttpClient client = new OkHttpClient();

    try

    {

    String res = new App().post(client,

    "http://localhost:8080/webbf/user/getUserList.do");

    System.out.println(res);

    }

    catch (IOException e)

    {

    e.printStackTrace();

    }

    }

    }

    结果打印

    请求成功,打印以下数据:

    {"userList":[{"id":49,"name":"876","address":"876"},{"id":50,"name":"antd","address":"antd"},{"id":51,"name":"sda","address":"sadsd"},{"id":52,"name":"5545","address":"4646546546"},{"id":53,"name":"sdas","address":"sdasa"},{"id":54,"name":"hggs","address":"sdsd"},{"id":55,"name":"4","address":"5"}, {"id":56,"name":"4","address":"4"},{"id":57,"name":"00ba9d8e-0628-4477-857f-ef617c1ff4bc","address":"5906"},{"id":58,"name":"613ee3a3-fb87-4413-a8e0-9272d10ad4a7","address":"6427"}]}

    了解详情请登陆昆明达内Java培训官网(km.Java.tedu.cn)!

    推荐文章

上一篇:OKHttp中的添加依赖

下一篇:Java继承和接口

最新开班日期  |  更多

Java--零基础全日制班

Java--零基础全日制班

开班日期:11/30

Java--零基础业余班

Java--零基础业余班

开班日期:11/30

Java--周末提升班

Java--周末提升班

开班日期:11/30

Java--零基础周末班

Java--零基础周末班

开班日期:11/30

  • 网址:http://km .java.tedu.cn      地址:昆明市官渡区春城路62号证券大厦附楼6楼
  • 课程培训电话:186 8716 1620      qq:2066486918    全国服务监督电话:400-827-0010
  • 服务邮箱 ts@tedu.cn
  • 2001-2016 达内国际公司(TARENA INTERNATIONAL,INC.) 版权所有 京ICP证08000853号-56