接着上节学习,带参数的post请求
刚开始的时候一直调试不通,刚开始的时候传参总是失败,发现是没有按照json的格式传参
解决方法:
在maven
中导入JSONObject
依赖,具体依赖网上可以找到
请求数据的方式:(部分代码)
OutputStream outputStream = connection.getOutputStream();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
JSONObject date = new JSONObject();
date.put("userName","admin");
date.put("password","96e79218965eb72c92a549dd5a330112");
String json = date.toString();
// System.out.println("打印得json:"+json);
out.writeBytes(json);
out.flush();
out.close();
请求数据解决了,返回数据又不知道怎么取,这可难为新手了(因为是登陆接口,所以要取返回的token
),经过一番斗争
通过JSONObject.fromObject
方法解决
解决方法:
InputStream inputStream = connection.getInputStream();
BufferedReader res = new BufferedReader(new InputStreamReader(inputStream));
String result1 = res.readLine();
System.out.println(result1);
JSONObject resultJson =JSONObject.fromObject(result1);
String res1 = resultJson.get("result").toString(); //取返回值
JSONObject TOKEN = JSONObject.fromObject(res1);//去返回值中得result值
result = TOKEN.get("token").toString();//去result中token值
完整的登陆请求:
public String login(){
String result = null;
try {
URL url = new URL("http://192.168.30.35:8080/v1/user/login");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");
// connection.setRequestProperty("accept","application/json");
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
//请求数据
// OutputStream outputStream = connection.getOutputStream();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
JSONObject date = new JSONObject();
date.put("userName","admin");
date.put("password","96e79218965eb72c92a549dd5a330112");
String json = date.toString();
// System.out.println("打印得json:"+json);
out.writeBytes(json);
out.flush();
out.close();
//返回
InputStream inputStream = connection.getInputStream();
BufferedReader res = new BufferedReader(new InputStreamReader(inputStream));
String result1 = res.readLine();
System.out.println(result1);
JSONObject resultJson =JSONObject.fromObject(result1);
String res1 = resultJson.get("result").toString(); //取返回值
JSONObject TOKEN = JSONObject.fromObject(res1);//去返回值中得result值
result = TOKEN.get("token").toString();//去result中token值
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
获取登陆请求返回的token
的新增请求:connection.setRequestProperty("Authorization",login());
调取登陆
public String addNotice(){
URL url = null;
String result1 = null;
try {
url = new URL("http://192.168.30.35:8080/v1/notice");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type","application/json");
connection.setRequestProperty("Authorization",login());
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
// connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
JSONObject date = new JSONObject();
date.put("content","内容内容");
date.put("disable",false);
date.put("showPopup",true);
date.put("title","AAA");
String json = date.toString();
// System.out.println("打印得json:"+json);
out.writeBytes(json);
out.flush();
out.close();
InputStream inputStream = connection.getInputStream();
BufferedReader res = new BufferedReader(new InputStreamReader(inputStream));
result1 = res.readLine();
System.out.println(result1);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result1;
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。