帮我写一段java 请求https后,将返回结果封装成jsonObject
时间: 2023-04-04 11:03:18 浏览: 103
Java使用JSONObject操作json实例解析
5星 · 资源好评率100%
好的,以下是Java请求HTTPS并将返回结果封装成JSONObject的代码示例:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import org.json.JSONObject;
public class HttpsRequest {
public static void main(String[] args) throws Exception {
String url = "https://example.com/api";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONObject jsonObject = new JSONObject(response.toString());
System.out.println(jsonObject);
}
}
```
这段代码可以向指定的HTTPS地址发送GET请求,并将返回结果封装成JSONObject对象。你可以根据实际情况修改URL和请求方法等参数。
阅读全文