安卓http get请求用法
时间: 2023-05-23 18:03:34 浏览: 67
安卓中可以使用 HttpClient 或 OkHttp 库进行 HTTP 请求。以下是使用 OkHttp 库进行 HTTP GET 请求的示例代码:
```
OkHttpClient client = new OkHttpClient();
String url = "http://example.com";
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
String body = response.body().string();
```
其中,url 为要请求的网址,Request.Builder() 方法用于构建请求,client.newCall(request) 方法发送请求并获取响应,response.body().string() 方法获取响应体的字符串形式。
需要注意的是,在 Android 9 及以上版本中,默认禁止了 HTTP cleartext traffic。如果要使用 HTTP 请求,请在 AndroidManifest.xml 文件中添加以下代码:
```
<application
...
android:usesCleartextTraffic="true">
...
</application>
```
相关问题
android封装http请求post和get
您好,Android 中可以使用 HttpURLConnection 类来进行 HTTP 请求的发送,以下是一个简单的封装示例:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpUtil {
public static String sendHttpRequest(String urlStr, String method, String data) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setDoInput(true);
connection.setDoOutput(true);
if (method.equals("POST")) {
OutputStream outputStream = connection.getOutputStream();
outputStream.write(data.getBytes());
outputStream.flush();
outputStream.close();
}
InputStream inputStream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
return response.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
return null;
}
}
```
您可以使用 `sendHttpRequest` 方法来发送 HTTP 请求,需要传入请求的 URL、请求方法(GET 或 POST)以及请求数据(如果是 POST 请求)。方法会返回响应的数据。例如,发送一个 GET 请求:
```java
String response = HttpUtil.sendHttpRequest("http://www.example.com/api", "GET", null);
```
发送一个 POST 请求:
```java
String data = "param1=value1¶m2=value2";
String response = HttpUtil.sendHttpRequest("http://www.example.com/api", "POST", data);
```
当然,上面的代码只是一个简单的封装示例,实际应用中需要进行更多的参数校验、错误处理等。
封装一个android http请求方法
以下是一个简单的Android HTTP请求方法的封装示例,使用了OkHttp库:
```java
import okhttp3.*;
public class HttpUtils {
private static final OkHttpClient client = new OkHttpClient();
public static String sendHttpRequest(String url) throws Exception {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new Exception("HTTP请求失败,错误码:" + response.code());
}
}
}
```
使用方法:
```java
try {
String response = HttpUtils.sendHttpRequest("http://example.com/api/data");
// 处理响应数据
} catch (Exception e) {
// 处理异常
}
```
以上示例仅供参考,实际应用中可能需要根据具体需求进行更改和完善。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)