温馨提示×

Android使用HttpClient发送请求

小亿
89
2023-12-21 12:53:48
栏目: 编程语言

在Android中发送请求可以使用HttpClient库。以下是一个使用HttpClient发送GET请求的示例代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

public class HttpClientExample {

    private static final int CONNECTION_TIMEOUT = 5000;
    private static final int SOCKET_TIMEOUT = 5000;

    public static String sendGetRequest(String url) throws IOException {
        // 创建HttpClient对象
        HttpClient httpClient = new DefaultHttpClient();
        // 创建HttpGet对象,并设置URL
        HttpGet httpGet = new HttpGet(url);
        
        // 设置超时时间
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, SOCKET_TIMEOUT);
        httpClient.setParams(httpParams);

        // 发送GET请求并获取响应
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream inputStream = httpEntity.getContent();

        // 将响应转换为字符串
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }
        bufferedReader.close();
        inputStream.close();

        return stringBuilder.toString();
    }

    public static String sendPostRequest(String url, List<NameValuePair> params) throws IOException {
        // 创建HttpClient对象
        HttpClient httpClient = new DefaultHttpClient();
        // 创建HttpPost对象,并设置URL
        HttpPost httpPost = new HttpPost(url);

        // 设置超时时间
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, SOCKET_TIMEOUT);
        httpClient.setParams(httpParams);

        // 设置POST请求的参数
        httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

        // 发送POST请求并获取响应
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream inputStream = httpEntity.getContent();

        // 将响应转换为字符串
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }
        bufferedReader.close();
        inputStream.close();

        return stringBuilder.toString();
    }

}

你可以使用sendGetRequest()方法发送GET请求,使用sendPostRequest()方法发送POST请求。传递的URL参数和POST请求的参数可以使用NameValuePair对象传递。

0