117.info
人生若只如初见

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

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe13dAzsLAwRUAVc.html

推荐文章

  • android页面怎样提高用户体验

    要提高Android页面的用户体验,可以从以下几个方面入手:
    网络优化 减少网络延迟:使用CDN、优化DNS解析、选择合适的网络协议(如HTTP/2或QUIC)、优化服务...

  • android页面适配不同屏幕尺寸方法

    在Android开发中,适配不同屏幕尺寸是一个重要的任务。以下是一些常用的方法和技巧:
    1. 使用相对布局(RelativeLayout)和约束布局(ConstraintLayout) R...

  • android页面怎样实现响应式设计

    在Android中实现响应式设计,可以通过以下几种方法: 使用ConstraintLayout布局:ConstraintLayout是一个灵活的布局管理器,它允许你通过约束来定位和调整视图的...

  • android页面如何优化加载速度

    优化Android页面加载速度是一个多方面的过程,涉及到布局、图片加载、内存管理等多个方面。以下是一些具体的优化技巧:
    布局优化 减少布局层级:使用Constr...

  • 如何使用notifyDataSetChanged

    notifyDataSetChanged() 是 RecyclerView.Adapter 类中的一个方法,用于通知 RecyclerView 数据集已更改,以便刷新视图。
    要使用 notifyDataSetChanged(),...

  • C# Oracle并非所有变量都已绑定

    在C#中,Oracle数据库并不需要将所有的变量都绑定。当执行SQL语句时,可以使用参数化查询来绑定特定的变量,以防止SQL注入攻击,并提高性能。
    使用参数化查...

  • C语言中的return true和return false是什么

    在C语言中,return true和return false是用于表示函数的返回值类型为布尔类型的情况。
    在C语言中,布尔类型的值只有两个,即真和假。通常情况下,true表示真...

  • scrapy爬虫无法翻页怎么办

    如果Scrapy爬虫无法翻页,可以尝试以下几个步骤来解决问题: 检查网页结构:确保目标网页的翻页链接是否存在,并且可以通过点击或修改URL来翻页。有时候网页使用...