WEB开发网
开发学院手机开发Android 开发 Android 网络开发详解 阅读

Android 网络开发详解

 2010-07-21 01:11:00 来源:WEB开发网   
核心提示:HttpClientParams.setRedirecting(params, true);// 设置 user agentHttpProtocolParams.setUserAgent(params, userAgent);// 创建一个 HttpClient 实例// 注意 HttpClient httpClien

HttpClientParams.setRedirecting(params, true);

// 设置 user agent

HttpProtocolParams.setUserAgent(params, userAgent);

// 创建一个 HttpClient 实例

// 注意 HttpClient httpClient = new HttpClient(); 是Commons HttpClient

// 中的用法,在 Android 1.5 中我们需要使用 Apache 的缺省实现 DefaultHttpClient

HttpClient httpClient = new DefaultHttpClient(params);

// 创建 HttpGet 方法,该方法会自动处理 URL 地址的重定向

HttpGet httpGet = new HttpGet ("http://www.test_test.com/");

HttpResponse response = client.execute(httpGet);

if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {

// 错误处理,例如可以在该请求正常结束前将其中断

httpGet.abort();

}

// 读取更多信息

Header[] headers = response.getHeaders();

HttpEntity entity = response.getEntity();

Header header = response.getFirstHeader("Content-Type");

} catch (Exception ee) {

//

} finally {

// 释放连接

client.getConnectionManager().shutdown();

}

import org.apache.http.HttpEntity;

import org.apache.http.HttpHost;

import org.apache.http.HttpResponse;

import org.apache.http.auth.AuthScope;

import org.apache.http.auth.UsernamePasswordCredentials;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.conn.params.ConnRoutePNames;

import org.apache.http.params. HttpConnectionParams;

import org.apache.http.client.params. HttpClientParams;

try {

// 创建 HttpParams 以用来设置 HTTP 参数(这一部分不是必需的)

HttpParams params = new BasicHttpParams();

// 设置连接超时和 Socket 超时,以及 Socket 缓存大小

HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);

HttpConnectionParams.setSoTimeout(params, 20 * 1000);

上一页  1 2 3 4 5  下一页

Tags:Android 网络 开发

编辑录入:coldstar [复制链接] [打 印]
赞助商链接