前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java基础学习-发送http请求接口关联

Java基础学习-发送http请求接口关联

原创
作者头像
FutureTester
发布2021-12-15 21:04:40
7650
发布2021-12-15 21:04:40
举报
文章被收录于专栏:FutureTester

接着上节学习,带参数的post请求

刚开始的时候一直调试不通,刚开始的时候传参总是失败,发现是没有按照json的格式传参

解决方法:

maven中导入JSONObject依赖,具体依赖网上可以找到

请求数据的方式:(部分代码)

代码语言:javascript
复制
            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方法解决

解决方法:

代码语言:javascript
复制
            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值

完整的登陆请求:

代码语言:javascript
复制
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());调取登陆

代码语言:javascript
复制
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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档