鏈式調用:
public class JunitTest {
@Test
public void test1() {
//1. 組建請求json參數
JSONObject json = new JSONObject();
json.put("appkey", "2e351b12c5030");
json.put("androidTitle", "PUSHDEMO");
Integer[] plat=new Integer[] {1};
json.put("plats",plat);
json.put("target", 4);
String[] registrationId=new String[] {"16772de0f6071dd4024b25de"};
json.put("registrationIds",registrationId);
json.put("content","test");
json.put("type", 1);
String url=" http://api.push.mob.com/v2/push";
/*發送post請求並接收響應數據
* 采用的是一種叫鏈式編程的方式):
header對應的是請求頭。
body對應的是請求體(包含參數和參數值)。
HttpRequest里面包含Post、GET、Delete、Put等常用的RestFul方式。*/
String post = HttpRequest.post(url)
.header("key","2e351b12c5030")
.header("sign","ee29e949d8c480a67e61e7921ac2dce7")
.body(json)
.execute().body();
System.out.println(post);
}
}
通過HttpRequest的方式比HttpUtil類里面的方法更加全面。幾乎類似於postman的功能
