获取get请求返回值

  Map<String, Object> map = new HashMap<>(8);
            map.put("uid", entity.getUid());
            String result = HttpURLConnectionUtil.get(IELTS_URL, map);
            JSONObject jsonObject = JSONObject.fromObject(result);
            Map<String,Object> map1 = (Map<String, Object>) jsonObject.get("data");
            phone = map1.get("phone").toString();
    public static String get(String url, Map<String, Object> param) {
        StringBuilder builder = new StringBuilder();
        try {
            StringBuilder params = new StringBuilder();
            for (Entry<String, Object> entry : param.entrySet()) {
                if (entry.getValue() != null) {
                    params.append(entry.getKey());
                    params.append("=");
                    params.append(entry.getValue().toString());
                    params.append("&");
                }
            }
            if (params.length() > 0) {
                params.deleteCharAt(params.lastIndexOf("&"));
            }
            URL restServiceURL = new URL(url + (params.length() > 0 ? "?" + params.toString() : ""));
            HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
            httpConnection.setRequestMethod("GET");
            httpConnection.setRequestProperty("Accept", "application/json");
            if (httpConnection.getResponseCode() != 200) {
                throw new RuntimeException("HTTP GET Request Failed with Error code : "
                        + httpConnection.getResponseCode());
            }
            InputStream inStrm = httpConnection.getInputStream();
            byte[] b = new byte[1024];
            int length = -1;
            while ((length = inStrm.read(b)) != -1) {
                builder.append(new String(b, 0, length));
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用HttpClient发送GET请求获取JSON数据,并将其输出到控制台。以下是一个示例代码: ``` import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URI; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.json.JSONObject; public class HttpClientExample { public static void main(String[] args) throws Exception { HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(); request.setURI(new URI("https://jsonplaceholder.typicode.com/todos/1")); HttpResponse response = client.execute(request); BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder result = new StringBuilder(); String line; while ((line = in.readLine()) != null) { result.append(line); } JSONObject jsonObject = new JSONObject(result.toString()); System.out.println(jsonObject.toString()); EntityUtils.consume(response.getEntity()); } } ``` 此代码将向`https://jsonplaceholder.typicode.com/todos/1`发送GET请求,并将返回的JSON数据输出到控制台。请注意,我们创建了一个`HttpClient`实例并使用它来执行请求。在响应中,我们读取实体内容并将其转换为字符串。然后,我们将字符串转换为`JSONObject`并将其输出到控制台。最后,我们将实体内容消耗掉,以便可以重复使用连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值