租用问题

质量为本、客户为根、勇于拼搏、务实创新

< 返回租用问题列表

Android使用HttpClient发送请求

发布时间:2023-12-21 12:53:48

Android使用HttpClient发送要求

在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对象传递。