昆明java培训
达内昆明广州春城路

18487146383

热门课程

OKHttp的简单使用

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

昆明达内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 IO流之字节流和字符流

java IO流之file类的常用操作

搭建james邮件服务器

Java识别操作系统

选择城市和中心
贵州省

广西省

海南省

扫一扫

了解更多干货