版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Android开发在线几个重要的知识点2博客分类:Android11.Android JSON解析示例代码如果远程服务器使用json而不是XML提供数据,则使用Android平台中已内置的org.json包可以简化Android平台的JSON解析过程。为了帮助以下Android123开发人员了解HTTP通信、正则表达式、JSON解析和appWidget开发,我们一起分析了此示例:Public class word构件extends appwidgetprovider /app构件Overridepublic void on update(context context、appwidgetman
2、ager appwidgetmanager、Int appWidgetIds) context . start service(new intent(context,update service . class);/因为您避免了ANR,所以小部件打开了服务public static class updateservice extends service OverridePublic void onstart (intent intent,int startid) /Build the widget update for todayremote views update views=build
3、update(this);component name this widget=new component name(this,word widget . class);appwidgetmanager=appwidgetmanager . getinstance(this);Manager.updateappwidget (this构件,update views);public remote views build update(context)/pick out month names from resourcesresources RES=context . get resources(
4、);stringmonthnames=RES . getstring array(r . array . month _ names);Time today=new Time();today . setto now();string pagename=RES . getstring(r . string . template _ wotd _ title,Month names today.month,today . month day);RemoteViews updateViews=nullString pageContent=Try simplewikihelper . prepare
5、user agent(context);page content=simple wiki helper . getpage content(page name,false); catch (ApiException e) log . e(“word构件”,“couldnt contact API”,e); catch (ParseException e) log . e(“word小部件”,“couldnt parse API响应”,e);pattern= pile(simple wiki helper . word _ of _ day _ regex);/正则表达式处
6、理,有关定义,请参阅下面的SimpleWikiHelper类matcher matcher=pattern . matcher(page content);If (matcher.find ()update views=new remote views(context . get package name()r . layout . widget _ word);string word title=matcher . group(1);update views . settextviewtext(r . id . word _ title,word title);update views .
7、settextviewtext(r . id . word _ type,matcher . group(2);update views . settextviewtext(r . id . definition,matcher.group (3)。trim();string define page=RES . getstring(r . string . template _ define _ URL,uri . encode(word title);intent define intent=new intent(intent . action _ view,uri . parse(defi
8、ne page);/其中相应的网页打开,因此Uri是http的URL,action是view。这意味着web浏览器打开pending intent pending intent=pending intent . get activity(context、0/* no request code */,define intent,0/* no flags */);update views . setonclickpendingintent(r . id . widget,pending intent);/单击小部件打开活动 elseupdate views=new remote views(con
9、text . get package name()r . layout . widget _ message);char sequence error message=context . get text(r . string . widget _ error);update views . settextviewtext(r . id . message,error message);Return updateViewsOverridepublic I binder on bind(intent intent)/web dont need to bind to this serviceRet
10、urn null网络通信的实体类和某些常量定义为:如下所示Public class simplewikihelperprivate static final string tag= simplewikihelper ;public static final string word _ of _ day _ regex=(?S)wotd|(.) |(.) | (# |)。*? “;private static final string wiktionary _ page=/w/api.php?action=query prop=revisions t
11、itles=% s RV prop=content format=JSON % s ;private static final string wiktionary _ expand _ templates=rvexpandtemplates=true ;private static final int http _ status _ ok=200;private static bytesbuffer=new byte512;private static string suser agent=null;public static class API exception extends excep
12、tion public API exception string detail messageSuper(detailMessage,throw able);public API exception(string detail message)super(detail message);public static class pars exception exception public parse exception(string detail message,throwdable throwdle)、Super(detailMessage,throw able);public static
13、 void prepare user agent(context context)Try /read package name and version number from manifestpackager manager=context . getpackagermanager();package info=manager . get packaging info(context . get package name()0);suser agent=string . format(context . getstring(r . string . template _ user _ agen
14、t),Info.package name,info.version name catch (namenotfoundexception)Log.e (tag, could nt find package information in package manager ,e);public static string getpage content(string title,boolean expand templates)Throws API exception,parseexceptionstring encoded title=uri . encode(title);string expand clause=expand templates?wik tionary _ expand _ tem
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五年度茶叶原料进出口代理合同
- 2025年度二零二五年度科技创新企业连带保证借款合同
- 2025年度解除终止劳动合同经济补偿争议处理办法
- 2025年度黄金质押融资担保抵押合同
- 2025年度金融机构跨境支付合作协议合同
- 二零二五年度普通员工竞业禁止及保密合同模板
- 二零二五年度文化活动赞助广告简易合同
- 酒店代理买卖合同
- 酒吧装修合同补充协议
- 人力资源招聘外包服务合同
- 2024-2025学年广东省深圳市南山区监测数学三年级第一学期期末学业水平测试试题含解析
- DB31∕731-2020 船舶修正总吨单位产品能源消耗限额
- 2024年卫生专业技术资格考试卫生检验技术(初级(师)211)相关专业知识试题及答案指导
- 江苏省南京鼓楼区2024年中考联考英语试题含答案
- 儿科护理学试题及答案解析-神经系统疾病患儿的护理(二)
- 15篇文章包含英语四级所有词汇
- 王阳明心学完整版本
- 四年级上册竖式计算300题及答案
- 课题研究实施方案 范例及课题研究方法及技术路线图模板
- 牙髓炎中牙髓干细胞与神经支配的相互作用
- 【2022届高考英语读后续写】主题升华积累讲义及高级句型积累
评论
0/150
提交评论