首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

童贞apk纠结过的技术点<1>

2012-09-14 
处女apk纠结过的技术点1????????????????????????????? 网络链接访问为题 android url访问主要分为两种:

处女apk纠结过的技术点<1>

????????????????????????????? 网络链接访问为题
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;
??}???
?}

?

热点排行