在Android 4.4.2上,您可以使用HttpURLConnection库来发送HTTPS请求。以下是一个使用HttpURLConnection解决问题的示例代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpsRequestExample {
public static void main(String[] args) {
String url = "https://restcountries.eu/rest/v2/all";
String response = sendHttpsGetRequest(url);
System.out.println(response);
}
public static String sendHttpsGetRequest(String url) {
StringBuilder response = new StringBuilder();
HttpURLConnection connection = null;
try {
URL requestUrl = new URL(url);
connection = (HttpURLConnection) requestUrl.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return response.toString();
}
}
请确保在您的Android项目中添加以下权限到AndroidManifest.xml
文件中:
以上代码将发送一个HTTPS GET请求到指定的URL,并返回服务器的响应。您可以将此代码集成到您的Android应用程序中,并根据需要进行调整。