首页主机资讯Android使用HttpClient发送请求

Android使用HttpClient发送请求

时间2023-12-21 14:53:03发布访客分类主机资讯浏览697
导读:在Android中发送请求可以使用HttpClient库。以下是一个使用HttpClient发送GET请求的示例代码: import java.io.BufferedReader; import java.io.IOException; i...

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

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Android使用HttpClient发送请求
本文地址: https://pptw.com/jishu/578706.html
win10开启vt最简单的步骤 C语言中的return true和return false是什么

游客 回复需填写必要信息