在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
對象傳遞。