亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

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
华亭县| 临安市| 叙永县| 舞阳县| 霍山县| 田东县| 东乡族自治县| 金乡县| 海安县| 射洪县| 屏东市| 天镇县| 济阳县| 宁国市| 绥棱县| 敖汉旗| 阳原县| 册亨县| 长葛市| 庆安县| 墨竹工卡县| 古蔺县| 大名县| 乐清市| 习水县| 通渭县| 民丰县| 乌审旗| 张家口市| 东丰县| 财经| 邯郸县| 西安市| 怀化市| 海安县| 正镶白旗| 尼勒克县| 高雄县| 长葛市| 民丰县| 铁岭县|