网络链接访问为题
android url访问主要分为两种:一种是httpurlconnection,另一种是httpclient,而前者只是简单的访问,不能设置参数,头文件等,而后者刚好弥补了前者的不足:

/**
* 执行一个HTTP GET请求,返回请求响应的HTML
*
* @param url
* 请求的URL地址
* @param queryString
* 请求的查询参数,可以为null
* @param charset
* 字符集
* @param pretty
* 是否美化
* @return 返回请求响应的HTML
*/
public static String doGetHttpHTML(String url, String cookie) {
StringBuffer response = new StringBuffer();
DefaultHttpClient client = new DefaultHttpClient();
HttpGet method = new HttpGet(url);
// 设置Http header数据
if(cookie != null){
String sessionId=StringUtils.cookieHandle(cookie.toString());
method.setHeader("Cookie",sessionId);
}
try {

HttpResponse res=client.execute(method);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity=res.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
String line;
while ((line = reader.readLine()) != null) {
response.append(line).append(System.getProperty("line.separator"));
}
reader.close();
}
return response.toString();
} catch(IOException e) {
Log.i("执行HTTP Get请求" + url + "时,发生异常!", e.toString());
return "InternetFail";
} finally {
client.getConnectionManager().shutdown();
}
}

以上获取返回值和httpurlconnection 功能差不多 ,一下这段代码获取cookie

/**
* 获取登陆Cookie
* @param urlpath
* @return
* @throws Exception
*/
public static String getData(String url){
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
try {
HttpResponse response = httpclient.execute(httpget);
CookieStore cookie=httpclient.getCookieStore();
return cookie.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

更多相关文章

  1. android 多个cursor 的读取 和读取某个Cursor其他从集合中获取的
  2. Android调用Camera,获取预览帧中的图像
  3. 启动uiautomatorviewer后,点击获取模拟器的时候,出现Unable to con
  4. Android代码优化----PullToRefresh+universal-image-loader实现
  5. Android(安卓)Volley运用
  6. Android低版本sdk的getSupportedPreviewFrameRates实现
  7. Android获取所有存储卡挂载路径
  8. Android从相册中获取图片以及路径
  9. 【Android】java.lang.IllegalArgumentException Illegal charac

随机推荐

  1. Android常见错误处理
  2. Android中BindService方式使用的理解
  3. Android材料设计
  4. Android(安卓)应用初始化及窗体事件的分
  5. Android(安卓)TabHost学习笔记
  6. android 原生的 MediaExtractor,MediaCod
  7. Android依赖注入:Google Guice on Android
  8. Android(安卓)中插件的编写方法
  9. Android(安卓)Studio Intent 页面切换崩
  10. Android(安卓)Recovery Ui 分析