androidhttpclient超时
1. android如何设置连接超时啊
您好,我是这样设置的,代码如下:
public static InputStream getHttpByApache(String urlStr) throws Exception {
HttpGet httpGet = new HttpGet(urlStr);
HttpClient httpClient = new DefaultHttpClient();
// 设置连接超时、读取超时
HttpParams basicHttpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(basicHttpParams, 3 * 1000);
HttpConnectionParams.setSoTimeout(basicHttpParams, 3 * 1000);
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return httpResponse.getEntity().getContent();
}
return null;
}
2. android java怎么设置超时时间
第一,ConnectionPoolTimeout:
定义了从ConnectionManager管理的连接池中取出连接的超时时间。
出错会抛出
第二,ConnectionTimeout:
定义了通过网络与服务器建立连接的超时时间,Httpclient包中通过一个异步线程去创建与服务器的socket连接,这就是该socket连接的超时时间。
当连接HTTP服务器或者等待HttpConnectionManager管理的一个有效连接超时出错会抛出ConnectionTimeoutException
第三,SocketTimeout:
这定义了Socket读数据的超时时间,即从服务器获取响应数据需要等待的时间。
当读取或者接收Socket超时会抛出SocketTimeoutException
3. android中http get请求总是超时怎么办
检查网络,查看是否有权限,httpclient框架实现示例代码:
1. GET 方式传递参数
//先将参数放入List,再对参数进行URL编码
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "数据")); //增加参数1
params.add(new BasicNameValuePair("param2", "value2"));//增加参数2
String param = URLEncodedUtils.format(params, "UTF-8");//对参数编码
String baseUrl = "服务器接口完整URL";
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//将URL与参数拼接
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //发起GET请求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//获取服务器响应内容
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2. POST方式 方式传递参数
//和GET方式一样,先将参数放入List
params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "Post方法"));//增加参数1
params.add(new BasicNameValuePair("param2", "第二个参数"));//增加参数2
try {
HttpPost postMethod = new HttpPost(baseUrl);//创建一个post请求
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //执行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //获取响应内容
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
4. 如何在Android开发中用HttpClient连接网络数据
HttpClient网络访问
一、HttpClient网络访问:
(一)、简介:
1、Apache组织提供了HttpClient项目,可以实现网络访问。在Android中,成功集成了HttpClient,所以在Android中可以直接使用HttpClient访问网络。
2、与HttpURLConnection相比,HttpClient将前者中的输入、输出流操作,统一封装成HttpGet、HttpPost、HttpRequest类。
HttpClient:网络连接对象;
HttpGet:代表发送GET请求;
HttpPost:代表发送POST请求;
HttpResponse:代表处理服务器响应的对象。
HttpEntity对象:该对象中包含了服务器所有的返回内容。
3、使用步骤:(六部曲)【重点】
创建HttpClient对象:通过实例化DefaultHttpClient获得;
创建HttpGet或HttpPost对象:通过实例化 HttpGet或HttpPost 获得,而构造方法的参数是urlstring(即需要访问的网络url地址)。也可以通过调用setParams()方法来添加请求参数;
调用HttpClient对象的execute()方法,参数是刚才创建的 HttpGet或HttpPost对象 ,返回值是HttpResponse对象;
通过response对象中的getStatusLine()方法和getStatusCode()方法获取服务器响应状态是否是200。
调用 HttpResponse对象的getEntity()方法,返回HttpEntity对象。而该对象中包含了服务器所有的返回内容。
借助EntityUtils的toString()方法或toByteArray()对 HttpEntity对象进行处理,也可以通过IO流对 HttpEntity对象进行操作。
(二)、封装HttpClientHelper工具类:
public class HttpClientHelper {
public static HttpClient checkNetwork(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(request);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
return httpClient;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:实现网络访问文件,将获取到数据储存在文件流中
*
* @param url
* :访问网络的url地址
* @return inputstream
*/
public static InputStream loadFileFromURL(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url);
HttpResponse httpResponse;
try {
httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
return entity.getContent();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:实现网络访问文件,将获取到的数据存在字节数组中
*
* @param url
* :访问网络的url地址
* @return byte[]
*/
public static byte[] loadByteFromURL(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("====>" + e.toString());
}
return null;
}
/**
* 作用:实现网络访问文件,返回字符串
*
* @param url
* :访问网络的url地址
* @return String
*/
public static String loadTextFromURL(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toString(httpEntity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:实现网络访问文件,先给服务器通过“GET”方式提交数据,再返回相应的数据
*
* @param url
* :访问网络的url地址
* @param params
* String url:访问url时,需要传递给服务器的参数。
* 第二个参数格式为:username=wangxiangjun&password=123456
* @return byte[]
*/
public static byte[] doGetSubmit(String url, String params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url + "?" + params);
try {
HttpResponse httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:实现网络访问文件,先给服务器通过“POST”方式提交数据,再返回相应的数据
*
* @param url
* :访问网络的url地址
* @param params
* String url:访问url时,需要传递给服务器的参数。 第二个参数为:List<NameValuePair>
* @return byte[]
*/
public static byte[] doPostSubmit(String url, List<NameValuePair> params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost requestPost = new HttpPost(url);
try {
requestPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
HttpResponse httpResponse = httpClient.execute(requestPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:实现网络访问文件,先给服务器通过“POST”方式提交数据,再返回相应的数据
*
* @param url
* :访问网络的url地址
* @param params
* String url:访问url时,需要传递给服务器的参数。 Map<String , Object>
* @return byte[]
*/
public static byte[] doPostSubmit(String url, Map<String, Object> params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost requestPost = new HttpPost(url);
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
try {
if (params != null) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
String key = entry.getKey();
String value = entry.getValue().toString();
BasicNameValuePair nameValuePair = new BasicNameValuePair(
key, value);
parameters.add(nameValuePair);
}
}
requestPost
.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
HttpResponse httpResponse = httpClient.execute(requestPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}