通过HttpURLConnection调用接口

2021-06-01 00:05

阅读:669

标签:tostring   new   NPU   col   tin   data   res   name   post   

 1 URL realurl  = new URL(REQUESTURL); //REQUESTURL为常量,请求地址
 2 HttpURLConnection httpURLConnection = (HttpURLConnection) realurl.openConnection();
 3 httpURLConnection.setRequestMethod("POST");
 4 httpURLConnection.setRequestProperty("Content-type","application/json");
 5 httpURLConnection.setDoInput(true);
 6 httpURLConnection.setDoOutput(true);
 7 DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream());
 8 out.write(info.toString().getBytes("UTF-8")); //info为JSONObject类型,为请求参数,格式为{"entity":{"id":"123456","name":"levi"}}
 9 out.flush();
10 out.close();
11 InputStream inputStream = httpURLConnection.getInputStream();   
12 String byteArrayOutputStream = streamToString(inputStream);    //streamToString为一个转化方法,将inputStream 类型转为String类型
13 JSONObject result = JSONObject.parseObject(byteArrayOutputStream);

 

通过HttpURLConnection调用接口

标签:tostring   new   NPU   col   tin   data   res   name   post   

原文地址:https://www.cnblogs.com/zeevy/p/14611006.html


评论


亲,登录后才可以留言!