中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

Android使用HttpClient發送請求

小億
82
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
攀枝花市| 若羌县| 七台河市| 申扎县| 永寿县| 沙洋县| 衡阳县| 黄骅市| 安多县| 河曲县| 黄平县| 武清区| 沁阳市| 曲靖市| 探索| 含山县| 灌云县| 江陵县| 仲巴县| 通州区| 双峰县| 长岛县| 寻乌县| 峡江县| 铁岭县| 通辽市| 浑源县| 屏东县| 札达县| 商水县| 莲花县| 琼中| 弋阳县| 星座| 湘潭县| 泽州县| 道孚县| 彰化市| 济宁市| 滨海县| 祁连县|