- 浏览: 554400 次
- 性别:
- 来自: 武汉
文章分类
- 全部博客 (533)
- spring (8)
- struts (21)
- hibernate (17)
- java其他 (73)
- 设计模式 (2)
- 开发软件/插件 (26)
- android (8)
- extjs4 (1)
- 网络编程 (4)
- 生活杂记 (3)
- ibatis (5)
- 应用服务器 (4)
- js (26)
- html/css (16)
- linux (0)
- db (32)
- jsp/servlet (13)
- xml (9)
- webservice (10)
- 错误/异常处理 (23)
- 线程 (10)
- maven (7)
- lucene (2)
- python (0)
- 报表 (1)
- mongodb (6)
- restful (6)
- ssl (1)
最新评论
-
zmwxiaoming:
...
struts2拦截器验证登陆状态 -
u012413283:
感谢楼主,同样的问题解决了
eclipse下安装m2e的maven插件报错的各类解决方案(含pom editor没装好的解决方案) -
javalucky:
你妹,想不吐槽都不行啊,eclipse 那来的maven4My ...
clipse加载maven工程提示pom.xml无法解析org.apache.maven.plugins:maven-resources-plugin: -
zhaoyh82:
感谢楼主
eclipse下安装m2e的maven插件报错的各类解决方案(含pom editor没装好的解决方案) -
hua2011:
按照楼主说的,还是没有出现pom editor编辑器,麻烦楼主 ...
eclipse下安装m2e的maven插件报错的各类解决方案(含pom editor没装好的解决方案)
android执行网络操作
本篇我们会介绍连接到网络中涉及的基本任务,监测的网络连接(包括连接更改),并给予用户控制应用程序的网络使用情况。还介绍了如何解析和使用XML数据。
这个类包含一个示例应用程序来说明如何执行常见的网络操作。您可以下载示例(在右边),并用它作为自己的应用程序源代码的可重用代码。本章的重点有三:
1.连接到网络
2.管理网络的使用
3.解析XML数据
一、连接到网络
在mainfest中声明权限,代码如下:
- <uses-permissionandroid:name="android.permission.INTERNET"/>
- <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
选择http客户端
大多数联网的Android应用程序使用HTTP来发送和接收数据。Android包括两个HTTP客户:HttpURLConnection HttpClient和Apache。都支持HTTPS,流媒体上传和下载,可配置的超时,IPv6,和连接池。我们建议使用HttpURLConnection目标应用程序。
检查网络连接
在你的应用程序尝试连接到网络,它应该检查是否一个网络连接可用使用getActiveNetworkInfo()和一个()。记住,这个装置可能范围的一个网络,或用户可能已经禁用wi - fi和移动数据访问。
- publicvoidmyClickHandler(Viewview){
- ...
- ConnectivityManagerconnMgr=(ConnectivityManager)
- getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfonetworkInfo=connMgr.getActiveNetworkInfo();
- if(networkInfo!=null&&networkInfo.isConnected()){
- //fetchdata
- }else{
- //displayerror
- }
- ...
- }
在单独线程中执行网络操作
网络操作可以包括不可预测的延迟。为了防止这种导致一个糟糕的用户体验,总是执行网络操作在一个单独的线程。
- publicclassHttpExampleActivityextendsActivity{
- privatestaticfinalStringDEBUG_TAG="HttpExample";
- privateEditTexturlText;
- privateTextViewtextView;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- urlText=(EditText)findViewById(R.id.myUrl);
- textView=(TextView)findViewById(R.id.myText);
- }
- //Whenuserclicksbutton,callsAsyncTask.
- //BeforeattemptingtofetchtheURL,makessurethatthereisanetworkconnection.
- publicvoidmyClickHandler(Viewview){
- //GetstheURLfromtheUI'stextfield.
- StringstringUrl=urlText.getText().toString();
- ConnectivityManagerconnMgr=(ConnectivityManager)
- getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfonetworkInfo=connMgr.getActiveNetworkInfo();
- if(networkInfo!=null&&networkInfo.isConnected()){
- newDownloadWebpageText().execute(stringUrl);
- }else{
- textView.setText("Nonetworkconnectionavailable.");
- }
- }
- //UsesAsyncTasktocreateataskawayfromthemainUIthread.Thistasktakesa
- //URLstringandusesittocreateanHttpUrlConnection.Oncetheconnection
- //hasbeenestablished,theAsyncTaskdownloadsthecontentsofthewebpageas
- //anInputStream.Finally,theInputStreamisconvertedintoastring,whichis
- //displayedintheUIbytheAsyncTask'sonPostExecutemethod.
- privateclassDownloadWebpageTextextendsAsyncTask{
- @Override
- protectedStringdoInBackground(String...urls){
- //paramscomesfromtheexecute()call:params[0]istheurl.
- try{
- returndownloadUrl(urls[0]);
- }catch(IOExceptione){
- return"Unabletoretrievewebpage.URLmaybeinvalid.";
- }
- }
- //onPostExecutedisplaystheresultsoftheAsyncTask.
- @Override
- protectedvoidonPostExecute(Stringresult){
- textView.setText(result);
- }
- }
- ...
- }
连接和下载数据
在你的线程执行您的网络交易,你可以使用HttpURLConnection来执行一个GET和下载数据。在您调用connect(),你可以得到一个InputStream的数据通过调用getInputStream()。
- //GivenaURL,establishesanHttpUrlConnectionandretrieves
- //thewebpagecontentasaInputStream,whichitreturnsas
- //astring.
- privateStringdownloadUrl(Stringmyurl)throwsIOException{
- InputStreamis=null;
- //Onlydisplaythefirst500charactersoftheretrieved
- //webpagecontent.
- intlen=500;
- try{
- URLurl=newURL(myurl);
- HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
- conn.setReadTimeout(10000/*milliseconds*/);
- conn.setConnectTimeout(15000/*milliseconds*/);
- conn.setRequestMethod("GET");
- conn.setDoInput(true);
- //Startsthequery
- conn.connect();
- intresponse=conn.getResponseCode();
- Log.d(DEBUG_TAG,"Theresponseis:"+response);
- is=conn.getInputStream();
- //ConverttheInputStreamintoastring
- StringcontentAsString=readIt(is,len);
- returncontentAsString;
- //MakessurethattheInputStreamisclosedaftertheappis
- //finishedusingit.
- }finally{
- if(is!=null){
- is.close();
- }
- }
- }
getResponseCode()返回连接的状态码。这是一种有用的方式获得一些额外的信息的连接。一个200的状态代码表示成功。
转换InputStream到String
- //ReadsanInputStreamandconvertsittoaString.
- publicStringreadIt(InputStreamstream,intlen)throwsIOException,UnsupportedEncodingException{
- Readerreader=null;
- reader=newInputStreamReader(stream,"UTF-8");
- char[]buffer=newchar[len];
- reader.read(buffer);
- returnnewString(buffer);
- }
二、管理网络
检查设备的网络连接
一个设备可以有各种类型的网络连接。这节课的重点是使用wi - fi或手机或网络连接,这个代码片段测试网络连接wi - fi和移动。它确定这些网络接口是可用的或连接的(即网络连接是否存在,如果可以建立套接字和传递数据)
- privatestaticfinalStringDEBUG_TAG="NetworkStatusExample";
- ...
- ConnectivityManagerconnMgr=(ConnectivityManager)
- getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfonetworkInfo=connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
- booleanisWifiConn=networkInfo.isConnected();
- networkInfo=connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
- booleanisMobileConn=networkInfo.isConnected();
- Log.d(DEBUG_TAG,"Wificonnected:"+isWifiConn);
- Log.d(DEBUG_TAG,"Mobileconnected:"+isMobileConn);
管理网络的使用
您可以实现一个首选项活动,让用户明确控制应用程序的使用网络资源。例如:1.你可能允许用户上传的视频只有当设备被连接到wi - fi网络。2.你可能会同步(或没有)根据特定标准如网络可用性、时间间隔,等等。
编写一个应用程序,支持网络访问和管理网络的使用,你的清单必须有正确的权限和意图过滤器。
在样例应用程序中,这个规定了SettingsActivity,将显示一个UI让用户知道何时可以进行下载操作。
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.android.networkusage"
- ...>
- <uses-sdkandroid:minSdkVersion="4"
- android:targetSdkVersion="14"/>
- <uses-permissionandroid:name="android.permission.INTERNET"/>
- <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
- <application
- ...>
- ...
- <activityandroid:label="SettingsActivity"android:name=".SettingsActivity">
- <intent-filter>
- <actionandroid:name="android.intent.action.MANAGE_NETWORK_USAGE"/>
- <categoryandroid:name="android.intent.category.DEFAULT"/>
- </intent-filter>
- </activity>
- </application>
- </manifest>
- publicclassSettingsActivityextendsPreferenceActivityimplementsOnSharedPreferenceChangeListener{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- //LoadstheXMLpreferencesfile
- addPreferencesFromResource(R.xml.preferences);
- }
- @Override
- protectedvoidonResume(){
- super.onResume();
- //Registersalistenerwheneverakeychanges
- getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
- }
- @Override
- protectedvoidonPause(){
- super.onPause();
- //UnregistersthelistenersetinonResume().
- //It'sbestpracticetounregisterlistenerswhenyourappisn'tusingthemtocutdownon
- //unnecessarysystemoverhead.YoudothisinonPause().
- getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
- }
- //Whentheuserchangesthepreferencesselection,
- //onSharedPreferenceChanged()restartsthemainactivityasanew
- //task.SetsthetherefreshDisplayflagto"true"toindicatethat
- //themainactivityshouldupdateitsdisplay.
- //ThemainactivityqueriesthePreferenceManagertogetthelatestsettings.
- @Override
- publicvoidonSharedPreferenceChanged(SharedPreferencessharedPreferences,Stringkey){
- //SetsrefreshDisplaytotruesothatwhentheuserreturnstothemain
- //activity,thedisplayrefreshestoreflectthenewsettings.
- NetworkActivity.refreshDisplay=true;
- }
- }
响应网络变动
如果有一个匹配发生在设置和设备的网络连接(例如,如果设置为“wi - fi”和设备有一个wi - fi连接)之间,应用程序下载提继续并刷新显示
- publicclassNetworkActivityextendsActivity{
- publicstaticfinalStringWIFI="Wi-Fi";
- publicstaticfinalStringANY="Any";
- privatestaticfinalStringURL="http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest";
- //WhetherthereisaWi-Ficonnection.
- privatestaticbooleanwifiConnected=false;
- //Whetherthereisamobileconnection.
- privatestaticbooleanmobileConnected=false;
- //Whetherthedisplayshouldberefreshed.
- publicstaticbooleanrefreshDisplay=true;
- //Theuser'scurrentnetworkpreferencesetting.
- publicstaticStringsPref=null;
- //TheBroadcastReceiverthattracksnetworkconnectivitychanges.
- privateNetworkReceiverreceiver=newNetworkReceiver();
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- //RegistersBroadcastReceivertotracknetworkconnectionchanges.
- IntentFilterfilter=newIntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
- receiver=newNetworkReceiver();
- this.registerReceiver(receiver,filter);
- }
- @Override
- publicvoidonDestroy(){
- super.onDestroy();
- //UnregistersBroadcastReceiverwhenappisdestroyed.
- if(receiver!=null){
- this.unregisterReceiver(receiver);
- }
- }
- //Refreshesthedisplayifthenetworkconnectionandthe
- //prefsettingsallowit.
- @Override
- publicvoidonStart(){
- super.onStart();
- //Getstheuser'snetworkpreferencesettings
- SharedPreferencessharedPrefs=PreferenceManager.getDefaultSharedPreferences(this);
- //Retrievesastringvalueforthepreferences.Thesecondparameter
- //isthedefaultvaluetouseifapreferencevalueisnotfound.
- sPref=sharedPrefs.getString("listPref","Wi-Fi");
- updateConnectedFlags();
- if(refreshDisplay){
- loadPage();
- }
- }
- //ChecksthenetworkconnectionandsetsthewifiConnectedandmobileConnected
- //variablesaccordingly.
- publicvoidupdateConnectedFlags(){
- ConnectivityManagerconnMgr=(ConnectivityManager)
- getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfoactiveInfo=connMgr.getActiveNetworkInfo();
- if(activeInfo!=null&&activeInfo.isConnected()){
- wifiConnected=activeInfo.getType()==ConnectivityManager.TYPE_WIFI;
- mobileConnected=activeInfo.getType()==ConnectivityManager.TYPE_MOBILE;
- }else{
- wifiConnected=false;
- mobileConnected=false;
- }
- }
- //UsesAsyncTasksubclasstodownloadtheXMLfeedfromstackoverflow.com.
- publicvoidloadPage(){
- if(((sPref.equals(ANY))&&(wifiConnected||mobileConnected))
- ||((sPref.equals(WIFI))&&(wifiConnected))){
- //AsyncTasksubclass
- newDownloadXmlTask().execute(URL);
- }else{
- showErrorPage();
- }
- }
- ...
- }
检测网络连接变化
- publicclassNetworkReceiverextendsBroadcastReceiver{
- @Override
- publicvoidonReceive(Contextcontext,Intentintent){
- ConnectivityManagerconn=(ConnectivityManager)
- context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfonetworkInfo=conn.getActiveNetworkInfo();
- //Checkstheuserprefsandthenetworkconnection.Basedontheresult,decideswhether
- //torefreshthedisplayorkeepthecurrentdisplay.
- //IftheuserprefisWi-Fionly,checkstoseeifthedevicehasaWi-Ficonnection.
- if(WIFI.equals(sPref)&&networkInfo!=null&&networkInfo.getType()==ConnectivityManager.TYPE_WIFI){
- //IfdevicehasitsWi-Ficonnection,setsrefreshDisplay
- //totrue.Thiscausesthedisplaytoberefreshedwhentheuser
- //returnstotheapp.
- refreshDisplay=true;
- Toast.makeText(context,R.string.wifi_connected,Toast.LENGTH_SHORT).show();
- //IfthesettingisANYnetworkandthereisanetworkconnection
- //(whichbyprocessofeliminationwouldbemobile),setsrefreshDisplaytotrue.
- }elseif(ANY.equals(sPref)&&networkInfo!=null){
- refreshDisplay=true;
- //Otherwise,theappcan'tdownloadcontent--eitherbecausethereisnonetwork
- //connection(mobileorWi-Fi),orbecausetheprefsettingisWIFI,andthere
- //isnoWi-Ficonnection.
- //SetsrefreshDisplaytofalse.
- }else{
- refreshDisplay=false;
- Toast.makeText(context,R.string.lost_connection,Toast.LENGTH_SHORT).show();
- }
- }
三、解析xml
上传和解析XML数据是很常见的任务,网络连接应用程序。这一课解释了如何解析XML文档并使用他们的数据
选择转换器
我们建议XmlPullParser,这是一种高效且可维护的方式来解析XML在Android里。
分析需求
- <?xmlversion="1.0"encoding="utf-8"?>
- <feedxmlns="http://www.w3.org/2005/Atom"xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"...">
- <titletype="text">newestquestionstaggedandroid-StackOverflow</title>
- ...
- <entry>
- ...
- </entry>
- <entry>
- <id>http://stackoverflow.com/q/9439999</id>
- <re:rankscheme="http://stackoverflow.com">0</re:rank>
- <titletype="text">Whereismydatafile?</title>
- <categoryscheme="http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest/tags"term="android"/>
- <categoryscheme="http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest/tags"term="file"/>
- <author>
- <name>cliff2310</name>
- <uri>http://stackoverflow.com/users/1128925</uri>
- </author>
- <linkrel="alternate"href="http://stackoverflow.com/questions/9439999/where-is-my-data-file"/>
- <published>2012-02-25T00:30:54Z</published>
- <updated>2012-02-25T00:30:54Z</updated>
- <summarytype="html">
- <p>IhaveanApplicationthatrequiresadatafile...</p>
- </summary>
- </entry>
- <entry>
- ...
- </entry>
- ...
- </feed>
实例化转化器
- publicclassStackOverflowXmlParser{
- //Wedon'tusenamespaces
- privatestaticfinalStringns=null;
- publicListparse(InputStreamin)throwsXmlPullParserException,IOException{
- try{
- XmlPullParserparser=Xml.newPullParser();
- parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,false);
- parser.setInput(in,null);
- parser.nextTag();
- returnreadFeed(parser);
- }finally{
- in.close();
- }
- }
- ...
- }
- 读取xml
- privateListreadFeed(XmlPullParserparser)throwsXmlPullParserException,IOException{
- Listentries=newArrayList();
- parser.require(XmlPullParser.START_TAG,ns,"feed");
- while(parser.next()!=XmlPullParser.END_TAG){
- if(parser.getEventType()!=XmlPullParser.START_TAG){
- continue;
- }
- Stringname=parser.getName();
- //Startsbylookingfortheentrytag
- if(name.equals("entry")){
- entries.add(readEntry(parser));
- }else{
- skip(parser);
- }
- }
- returnentries;
- }
下面这个代码片段展示了如何解析器解析条目、标题、链接和总结:
- publicstaticclassEntry{
- publicfinalStringtitle;
- publicfinalStringlink;
- publicfinalStringsummary;
- privateEntry(Stringtitle,Stringsummary,Stringlink){
- this.title=title;
- this.summary=summary;
- this.link=link;
- }
- }
- //Parsesthecontentsofanentry.Ifitencountersatitle,summary,orlinktag,handsthemoff
- //totheirrespective"read"methodsforprocessing.Otherwise,skipsthetag.
- privateEntryreadEntry(XmlPullParserparser)throwsXmlPullParserException,IOException{
- parser.require(XmlPullParser.START_TAG,ns,"entry");
- Stringtitle=null;
- Stringsummary=null;
- Stringlink=null;
- while(parser.next()!=XmlPullParser.END_TAG){
- if(parser.getEventType()!=XmlPullParser.START_TAG){
- continue;
- }
- Stringname=parser.getName();
- if(name.equals("title")){
- title=readTitle(parser);
- }elseif(name.equals("summary")){
- summary=readSummary(parser);
- }elseif(name.equals("link")){
- link=readLink(parser);
- }else{
- skip(parser);
- }
- }
- returnnewEntry(title,summary,link);
- }
- //Processestitletagsinthefeed.
- privateStringreadTitle(XmlPullParserparser)throwsIOException,XmlPullParserException{
- parser.require(XmlPullParser.START_TAG,ns,"title");
- Stringtitle=readText(parser);
- parser.require(XmlPullParser.END_TAG,ns,"title");
- returntitle;
- }
- //Processeslinktagsinthefeed.
- privateStringreadLink(XmlPullParserparser)throwsIOException,XmlPullParserException{
- Stringlink="";
- parser.require(XmlPullParser.START_TAG,ns,"link");
- Stringtag=parser.getName();
- StringrelType=parser.getAttributeValue(null,"rel");
- if(tag.equals("link")){
- if(relType.equals("alternate")){
- link=parser.getAttributeValue(null,"href");
- parser.nextTag();
- }
- }
- parser.require(XmlPullParser.END_TAG,ns,"link");
- returnlink;
- }
- //Processessummarytagsinthefeed.
- privateStringreadSummary(XmlPullParserparser)throwsIOException,XmlPullParserException{
- parser.require(XmlPullParser.START_TAG,ns,"summary");
- Stringsummary=readText(parser);
- parser.require(XmlPullParser.END_TAG,ns,"summary");
- returnsummary;
- }
- //Forthetagstitleandsummary,extractstheirtextvalues.
- privateStringreadText(XmlPullParserparser)throwsIOException,XmlPullParserException{
- Stringresult="";
- if(parser.next()==XmlPullParser.TEXT){
- result=parser.getText();
- parser.nextTag();
- }
- returnresult;
- }
- ...
- }
跳过无用的标签
- privatevoidskip(XmlPullParserparser)throwsXmlPullParserException,IOException{
- if(parser.getEventType()!=XmlPullParser.START_TAG){
- thrownewIllegalStateException();
- }
- intdepth=1;
- while(depth!=0){
- switch(parser.next()){
- caseXmlPullParser.END_TAG:
- depth--;
- break;
- caseXmlPullParser.START_TAG:
- depth++;
- break;
- }
- }
- }
使用xml数据
- publicclassNetworkActivityextendsActivity{
- publicstaticfinalStringWIFI="Wi-Fi";
- publicstaticfinalStringANY="Any";
- privatestaticfinalStringURL="http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest";
- //WhetherthereisaWi-Ficonnection.
- privatestaticbooleanwifiConnected=false;
- //Whetherthereisamobileconnection.
- privatestaticbooleanmobileConnected=false;
- //Whetherthedisplayshouldberefreshed.
- publicstaticbooleanrefreshDisplay=true;
- publicstaticStringsPref=null;
- ...
- //UsesAsyncTasktodownloadtheXMLfeedfromstackoverflow.com.
- publicvoidloadPage(){
- if((sPref.equals(ANY))&&(wifiConnected||mobileConnected)){
- newDownloadXmlTask().execute(URL);
- }
- elseif((sPref.equals(WIFI))&&(wifiConnected)){
- newDownloadXmlTask().execute(URL);
- }else{
- //showerror
- }
- }
动态下载xml
- //ImplementationofAsyncTaskusedtodownloadXMLfeedfromstackoverflow.com.
- privateclassDownloadXmlTaskextendsAsyncTask<String,Void,String>{
- @Override
- protectedStringdoInBackground(String...urls){
- try{
- returnloadXmlFromNetwork(urls[0]);
- }catch(IOExceptione){
- returngetResources().getString(R.string.connection_error);
- }catch(XmlPullParserExceptione){
- returngetResources().getString(R.string.xml_error);
- }
- }
- @Override
- protectedvoidonPostExecute(Stringresult){
- setContentView(R.layout.main);
- //DisplaystheHTMLstringintheUIviaaWebView
- WebViewmyWebView=(WebView)findViewById(R.id.webview);
- myWebView.loadData(result,"text/html",null);
- }
- }
加载xml
- //UploadsXMLfromstackoverflow.com,parsesit,andcombinesitwith
- //HTMLmarkup.ReturnsHTMLstring.
- privateStringloadXmlFromNetwork(StringurlString)throwsXmlPullParserException,IOException{
- InputStreamstream=null;
- //Instantiatetheparser
- StackOverflowXmlParserstackOverflowXmlParser=newStackOverflowXmlParser();
- List<Entry>entries=null;
- Stringtitle=null;
- Stringurl=null;
- Stringsummary=null;
- CalendarrightNow=Calendar.getInstance();
- DateFormatformatter=newSimpleDateFormat("MMMddh:mmaa");
- //Checkswhethertheusersetthepreferencetoincludesummarytext
- SharedPreferencessharedPrefs=PreferenceManager.getDefaultSharedPreferences(this);
- booleanpref=sharedPrefs.getBoolean("summaryPref",false);
- StringBuilderhtmlString=newStringBuilder();
- htmlString.append("<h3>"+getResources().getString(R.string.page_title)+"</h3>");
- htmlString.append("<em>"+getResources().getString(R.string.updated)+""+
- formatter.format(rightNow.getTime())+"</em>");
- try{
- stream=downloadUrl(urlString);
- entries=stackOverflowXmlParser.parse(stream);
- //MakessurethattheInputStreamisclosedaftertheappis
- //finishedusingit.
- }finally{
- if(stream!=null){
- stream.close();
- }
- }
- //StackOverflowXmlParserreturnsaList(called"entries")ofEntryobjects.
- //EachEntryobjectrepresentsasinglepostintheXMLfeed.
- //ThissectionprocessestheentrieslisttocombineeachentrywithHTMLmarkup.
- //EachentryisdisplayedintheUIasalinkthatoptionallyincludes
- //atextsummary.
- for(Entryentry:entries){
- htmlString.append("<p><ahref='");
- htmlString.append(entry.link);
- htmlString.append("'>"+entry.title+"</a></p>");
- //Iftheusersetthepreferencetoincludesummarytext,
- //addsittothedisplay.
- if(pref){
- htmlString.append(entry.summary);
- }
- }
- returnhtmlString.toString();
- }
- //GivenastringrepresentationofaURL,setsupaconnectionandgets
- //aninputstream.
- privateInputStreamdownloadUrl(StringurlString)throwsIOException{
- URLurl=newURL(urlString);
- HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
- conn.setReadTimeout(10000/*milliseconds*/);
- conn.setConnectTimeout(15000/*milliseconds*/);
- conn.setRequestMethod("GET");
- conn.setDoInput(true);
- //Startsthequery
- conn.connect();
- InputStreamstream=conn.getInputStream();
- }
上面的片段代码为了帮助大家理解,这里还是老习惯,贴上项目的源代码,本来是有项目截图,运行效果图之类的图的,只是这篇文章太长了,再弄图片,就更占篇幅了,所以就只贴源码了,大家可以自己的运行起来看看,希望能从整体的架构和具体的代码细节上帮助到大家。
本篇我们会介绍连接到网络中涉及的基本任务,监测的网络连接(包括连接更改),并给予用户控制应用程序的网络使用情况。还介绍了如何解析和使用XML数据。这个类包含一个示例应用程序来说明如何执行常见的网络操作。您可以下载示例(在右边),并用它作为自己的应用程序源代码的可重用代码。本章的重点有三:
1.连接到网络
2.管理网络的使用
3.解析XML数据
一、连接到网络
在mainfest中声明权限,代码如下:
- <uses-permissionandroid:name="android.permission.INTERNET"/>
- <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
选择http客户端
大多数联网的Android应用程序使用HTTP来发送和接收数据。Android包括两个HTTP客户:HttpURLConnection HttpClient和Apache。都支持HTTPS,流媒体上传和下载,可配置的超时,IPv6,和连接池。我们建议使用HttpURLConnection目标应用程序。
检查网络连接
在你的应用程序尝试连接到网络,它应该检查是否一个网络连接可用使用getActiveNetworkInfo()和一个()。记住,这个装置可能范围的一个网络,或用户可能已经禁用wi - fi和移动数据访问。
- publicvoidmyClickHandler(Viewview){
- ...
- ConnectivityManagerconnMgr=(ConnectivityManager)
- getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfonetworkInfo=connMgr.getActiveNetworkInfo();
- if(networkInfo!=null&&networkInfo.isConnected()){
- //fetchdata
- }else{
- //displayerror
- }
- ...
- }
在单独线程中执行网络操作
网络操作可以包括不可预测的延迟。为了防止这种导致一个糟糕的用户体验,总是执行网络操作在一个单独的线程。
- publicclassHttpExampleActivityextendsActivity{
- privatestaticfinalStringDEBUG_TAG="HttpExample";
- privateEditTexturlText;
- privateTextViewtextView;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- urlText=(EditText)findViewById(R.id.myUrl);
- textView=(TextView)findViewById(R.id.myText);
- }
- //Whenuserclicksbutton,callsAsyncTask.
- //BeforeattemptingtofetchtheURL,makessurethatthereisanetworkconnection.
- publicvoidmyClickHandler(Viewview){
- //GetstheURLfromtheUI'stextfield.
- StringstringUrl=urlText.getText().toString();
- ConnectivityManagerconnMgr=(ConnectivityManager)
- getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfonetworkInfo=connMgr.getActiveNetworkInfo();
- if(networkInfo!=null&&networkInfo.isConnected()){
- newDownloadWebpageText().execute(stringUrl);
- }else{
- textView.setText("Nonetworkconnectionavailable.");
- }
- }
- //UsesAsyncTasktocreateataskawayfromthemainUIthread.Thistasktakesa
- //URLstringandusesittocreateanHttpUrlConnection.Oncetheconnection
- //hasbeenestablished,theAsyncTaskdownloadsthecontentsofthewebpageas
- //anInputStream.Finally,theInputStreamisconvertedintoastring,whichis
- //displayedintheUIbytheAsyncTask'sonPostExecutemethod.
- privateclassDownloadWebpageTextextendsAsyncTask{
- @Override
- protectedStringdoInBackground(String...urls){
- //paramscomesfromtheexecute()call:params[0]istheurl.
- try{
- returndownloadUrl(urls[0]);
- }catch(IOExceptione){
- return"Unabletoretrievewebpage.URLmaybeinvalid.";
- }
- }
- //onPostExecutedisplaystheresultsoftheAsyncTask.
- @Override
- protectedvoidonPostExecute(Stringresult){
- textView.setText(result);
- }
- }
- ...
- }
连接和下载数据
在你的线程执行您的网络交易,你可以使用HttpURLConnection来执行一个GET和下载数据。在您调用connect(),你可以得到一个InputStream的数据通过调用getInputStream()。
- //GivenaURL,establishesanHttpUrlConnectionandretrieves
- //thewebpagecontentasaInputStream,whichitreturnsas
- //astring.
- privateStringdownloadUrl(Stringmyurl)throwsIOException{
- InputStreamis=null;
- //Onlydisplaythefirst500charactersoftheretrieved
- //webpagecontent.
- intlen=500;
- try{
- URLurl=newURL(myurl);
- HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
- conn.setReadTimeout(10000/*milliseconds*/);
- conn.setConnectTimeout(15000/*milliseconds*/);
- conn.setRequestMethod("GET");
- conn.setDoInput(true);
- //Startsthequery
- conn.connect();
- intresponse=conn.getResponseCode();
- Log.d(DEBUG_TAG,"Theresponseis:"+response);
- is=conn.getInputStream();
- //ConverttheInputStreamintoastring
- StringcontentAsString=readIt(is,len);
- returncontentAsString;
- //MakessurethattheInputStreamisclosedaftertheappis
- //finishedusingit.
- }finally{
- if(is!=null){
- is.close();
- }
- }
- }
getResponseCode()返回连接的状态码。这是一种有用的方式获得一些额外的信息的连接。一个200的状态代码表示成功。
转换InputStream到String
- //ReadsanInputStreamandconvertsittoaString.
- publicStringreadIt(InputStreamstream,intlen)throwsIOException,UnsupportedEncodingException{
- Readerreader=null;
- reader=newInputStreamReader(stream,"UTF-8");
- char[]buffer=newchar[len];
- reader.read(buffer);
- returnnewString(buffer);
- }
二、管理网络
检查设备的网络连接
一个设备可以有各种类型的网络连接。这节课的重点是使用wi - fi或手机或网络连接,这个代码片段测试网络连接wi - fi和移动。它确定这些网络接口是可用的或连接的(即网络连接是否存在,如果可以建立套接字和传递数据)
- privatestaticfinalStringDEBUG_TAG="NetworkStatusExample";
- ...
- ConnectivityManagerconnMgr=(ConnectivityManager)
- getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfonetworkInfo=connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
- booleanisWifiConn=networkInfo.isConnected();
- networkInfo=connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
- booleanisMobileConn=networkInfo.isConnected();
- Log.d(DEBUG_TAG,"Wificonnected:"+isWifiConn);
- Log.d(DEBUG_TAG,"Mobileconnected:"+isMobileConn);
管理网络的使用
您可以实现一个首选项活动,让用户明确控制应用程序的使用网络资源。例如:1.你可能允许用户上传的视频只有当设备被连接到wi - fi网络。2.你可能会同步(或没有)根据特定标准如网络可用性、时间间隔,等等。
编写一个应用程序,支持网络访问和管理网络的使用,你的清单必须有正确的权限和意图过滤器。
在样例应用程序中,这个规定了SettingsActivity,将显示一个UI让用户知道何时可以进行下载操作。
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.android.networkusage"
- ...>
- <uses-sdkandroid:minSdkVersion="4"
- android:targetSdkVersion="14"/>
- <uses-permissionandroid:name="android.permission.INTERNET"/>
- <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
- <application
- ...>
- ...
- <activityandroid:label="SettingsActivity"android:name=".SettingsActivity">
- <intent-filter>
- <actionandroid:name="android.intent.action.MANAGE_NETWORK_USAGE"/>
- <categoryandroid:name="android.intent.category.DEFAULT"/>
- </intent-filter>
- </activity>
- </application>
- </manifest>
- publicclassSettingsActivityextendsPreferenceActivityimplementsOnSharedPreferenceChangeListener{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- //LoadstheXMLpreferencesfile
- addPreferencesFromResource(R.xml.preferences);
- }
- @Override
- protectedvoidonResume(){
- super.onResume();
- //Registersalistenerwheneverakeychanges
- getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
- }
- @Override
- protectedvoidonPause(){
- super.onPause();
- //UnregistersthelistenersetinonResume().
- //It'sbestpracticetounregisterlistenerswhenyourappisn'tusingthemtocutdownon
- //unnecessarysystemoverhead.YoudothisinonPause().
- getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
- }
- //Whentheuserchangesthepreferencesselection,
- //onSharedPreferenceChanged()restartsthemainactivityasanew
- //task.SetsthetherefreshDisplayflagto"true"toindicatethat
- //themainactivityshouldupdateitsdisplay.
- //ThemainactivityqueriesthePreferenceManagertogetthelatestsettings.
- @Override
- publicvoidonSharedPreferenceChanged(SharedPreferencessharedPreferences,Stringkey){
- //SetsrefreshDisplaytotruesothatwhentheuserreturnstothemain
- //activity,thedisplayrefreshestoreflectthenewsettings.
- NetworkActivity.refreshDisplay=true;
- }
- }
响应网络变动
如果有一个匹配发生在设置和设备的网络连接(例如,如果设置为“wi - fi”和设备有一个wi - fi连接)之间,应用程序下载提继续并刷新显示
- publicclassNetworkActivityextendsActivity{
- publicstaticfinalStringWIFI="Wi-Fi";
- publicstaticfinalStringANY="Any";
- privatestaticfinalStringURL="http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest";
- //WhetherthereisaWi-Ficonnection.
- privatestaticbooleanwifiConnected=false;
- //Whetherthereisamobileconnection.
- privatestaticbooleanmobileConnected=false;
- //Whetherthedisplayshouldberefreshed.
- publicstaticbooleanrefreshDisplay=true;
- //Theuser'scurrentnetworkpreferencesetting.
- publicstaticStringsPref=null;
- //TheBroadcastReceiverthattracksnetworkconnectivitychanges.
- privateNetworkReceiverreceiver=newNetworkReceiver();
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- //RegistersBroadcastReceivertotracknetworkconnectionchanges.
- IntentFilterfilter=newIntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
- receiver=newNetworkReceiver();
- this.registerReceiver(receiver,filter);
- }
- @Override
- publicvoidonDestroy(){
- super.onDestroy();
- //UnregistersBroadcastReceiverwhenappisdestroyed.
- if(receiver!=null){
- this.unregisterReceiver(receiver);
- }
- }
- //Refreshesthedisplayifthenetworkconnectionandthe
- //prefsettingsallowit.
- @Override
- publicvoidonStart(){
- super.onStart();
- //Getstheuser'snetworkpreferencesettings
- SharedPreferencessharedPrefs=PreferenceManager.getDefaultSharedPreferences(this);
- //Retrievesastringvalueforthepreferences.Thesecondparameter
- //isthedefaultvaluetouseifapreferencevalueisnotfound.
- sPref=sharedPrefs.getString("listPref","Wi-Fi");
- updateConnectedFlags();
- if(refreshDisplay){
- loadPage();
- }
- }
- //ChecksthenetworkconnectionandsetsthewifiConnectedandmobileConnected
- //variablesaccordingly.
- publicvoidupdateConnectedFlags(){
- ConnectivityManagerconnMgr=(ConnectivityManager)
- getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfoactiveInfo=connMgr.getActiveNetworkInfo();
- if(activeInfo!=null&&activeInfo.isConnected()){
- wifiConnected=activeInfo.getType()==ConnectivityManager.TYPE_WIFI;
- mobileConnected=activeInfo.getType()==ConnectivityManager.TYPE_MOBILE;
- }else{
- wifiConnected=false;
- mobileConnected=false;
- }
- }
- //UsesAsyncTasksubclasstodownloadtheXMLfeedfromstackoverflow.com.
- publicvoidloadPage(){
- if(((sPref.equals(ANY))&&(wifiConnected||mobileConnected))
- ||((sPref.equals(WIFI))&&(wifiConnected))){
- //AsyncTasksubclass
- newDownloadXmlTask().execute(URL);
- }else{
- showErrorPage();
- }
- }
- ...
- }
检测网络连接变化
- publicclassNetworkReceiverextendsBroadcastReceiver{
- @Override
- publicvoidonReceive(Contextcontext,Intentintent){
- ConnectivityManagerconn=(ConnectivityManager)
- context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfonetworkInfo=conn.getActiveNetworkInfo();
- //Checkstheuserprefsandthenetworkconnection.Basedontheresult,decideswhether
- //torefreshthedisplayorkeepthecurrentdisplay.
- //IftheuserprefisWi-Fionly,checkstoseeifthedevicehasaWi-Ficonnection.
- if(WIFI.equals(sPref)&&networkInfo!=null&&networkInfo.getType()==ConnectivityManager.TYPE_WIFI){
- //IfdevicehasitsWi-Ficonnection,setsrefreshDisplay
- //totrue.Thiscausesthedisplaytoberefreshedwhentheuser
- //returnstotheapp.
- refreshDisplay=true;
- Toast.makeText(context,R.string.wifi_connected,Toast.LENGTH_SHORT).show();
- //IfthesettingisANYnetworkandthereisanetworkconnection
- //(whichbyprocessofeliminationwouldbemobile),setsrefreshDisplaytotrue.
- }elseif(ANY.equals(sPref)&&networkInfo!=null){
- refreshDisplay=true;
- //Otherwise,theappcan'tdownloadcontent--eitherbecausethereisnonetwork
- //connection(mobileorWi-Fi),orbecausetheprefsettingisWIFI,andthere
- //isnoWi-Ficonnection.
- //SetsrefreshDisplaytofalse.
- }else{
- refreshDisplay=false;
- Toast.makeText(context,R.string.lost_connection,Toast.LENGTH_SHORT).show();
- }
- }
三、解析xml
上传和解析XML数据是很常见的任务,网络连接应用程序。这一课解释了如何解析XML文档并使用他们的数据
选择转换器
我们建议XmlPullParser,这是一种高效且可维护的方式来解析XML在Android里。
分析需求
- <?xmlversion="1.0"encoding="utf-8"?>
- <feedxmlns="http://www.w3.org/2005/Atom"xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"...">
- <titletype="text">newestquestionstaggedandroid-StackOverflow</title>
- ...
- <entry>
- ...
- </entry>
- <entry>
- <id>http://stackoverflow.com/q/9439999</id>
- <re:rankscheme="http://stackoverflow.com">0</re:rank>
- <titletype="text">Whereismydatafile?</title>
- <categoryscheme="http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest/tags"term="android"/>
- <categoryscheme="http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest/tags"term="file"/>
- <author>
- <name>cliff2310</name>
- <uri>http://stackoverflow.com/users/1128925</uri>
- </author>
- <linkrel="alternate"href="http://stackoverflow.com/questions/9439999/where-is-my-data-file"/>
- <published>2012-02-25T00:30:54Z</published>
- <updated>2012-02-25T00:30:54Z</updated>
- <summarytype="html">
- <p>IhaveanApplicationthatrequiresadatafile...</p>
- </summary>
- </entry>
- <entry>
- ...
- </entry>
- ...
- </feed>
实例化转化器
- publicclassStackOverflowXmlParser{
- //Wedon'tusenamespaces
- privatestaticfinalStringns=null;
- publicListparse(InputStreamin)throwsXmlPullParserException,IOException{
- try{
- XmlPullParserparser=Xml.newPullParser();
- parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,false);
- parser.setInput(in,null);
- parser.nextTag();
- returnreadFeed(parser);
- }finally{
- in.close();
- }
- }
- ...
- }
- 读取xml
- privateListreadFeed(XmlPullParserparser)throwsXmlPullParserException,IOException{
- Listentries=newArrayList();
- parser.require(XmlPullParser.START_TAG,ns,"feed");
- while(parser.next()!=XmlPullParser.END_TAG){
- if(parser.getEventType()!=XmlPullParser.START_TAG){
- continue;
- }
- Stringname=parser.getName();
- //Startsbylookingfortheentrytag
- if(name.equals("entry")){
- entries.add(readEntry(parser));
- }else{
- skip(parser);
- }
- }
- returnentries;
- }
下面这个代码片段展示了如何解析器解析条目、标题、链接和总结:
- publicstaticclassEntry{
- publicfinalStringtitle;
- publicfinalStringlink;
- publicfinalStringsummary;
- privateEntry(Stringtitle,Stringsummary,Stringlink){
- this.title=title;
- this.summary=summary;
- this.link=link;
- }
- }
- //Parsesthecontentsofanentry.Ifitencountersatitle,summary,orlinktag,handsthemoff
- //totheirrespective"read"methodsforprocessing.Otherwise,skipsthetag.
- privateEntryreadEntry(XmlPullParserparser)throwsXmlPullParserException,IOException{
- parser.require(XmlPullParser.START_TAG,ns,"entry");
- Stringtitle=null;
- Stringsummary=null;
- Stringlink=null;
- while(parser.next()!=XmlPullParser.END_TAG){
- if(parser.getEventType()!=XmlPullParser.START_TAG){
- continue;
- }
- Stringname=parser.getName();
- if(name.equals("title")){
- title=readTitle(parser);
- }elseif(name.equals("summary")){
- summary=readSummary(parser);
- }elseif(name.equals("link")){
- link=readLink(parser);
- }else{
- skip(parser);
- }
- }
- returnnewEntry(title,summary,link);
- }
- //Processestitletagsinthefeed.
- privateStringreadTitle(XmlPullParserparser)throwsIOException,XmlPullParserException{
- parser.require(XmlPullParser.START_TAG,ns,"title");
- Stringtitle=readText(parser);
- parser.require(XmlPullParser.END_TAG,ns,"title");
- returntitle;
- }
- //Processeslinktagsinthefeed.
- privateStringreadLink(XmlPullParserparser)throwsIOException,XmlPullParserException{
- Stringlink="";
- parser.require(XmlPullParser.START_TAG,ns,"link");
- Stringtag=parser.getName();
- StringrelType=parser.getAttributeValue(null,"rel");
- if(tag.equals("link")){
- if(relType.equals("alternate")){
- link=parser.getAttributeValue(null,"href");
- parser.nextTag();
- }
- }
- parser.require(XmlPullParser.END_TAG,ns,"link");
- returnlink;
- }
- //Processessummarytagsinthefeed.
- privateStringreadSummary(XmlPullParserparser)throwsIOException,XmlPullParserException{
- parser.require(XmlPullParser.START_TAG,ns,"summary");
- Stringsummary=readText(parser);
- parser.require(XmlPullParser.END_TAG,ns,"summary");
- returnsummary;
- }
- //Forthetagstitleandsummary,extractstheirtextvalues.
- privateStringreadText(XmlPullParserparser)throwsIOException,XmlPullParserException{
- Stringresult="";
- if(parser.next()==XmlPullParser.TEXT){
- result=parser.getText();
- parser.nextTag();
- }
- returnresult;
- }
- ...
- }
跳过无用的标签
- privatevoidskip(XmlPullParserparser)throwsXmlPullParserException,IOException{
- if(parser.getEventType()!=XmlPullParser.START_TAG){
- thrownewIllegalStateException();
- }
- intdepth=1;
- while(depth!=0){
- switch(parser.next()){
- caseXmlPullParser.END_TAG:
- depth--;
- break;
- caseXmlPullParser.START_TAG:
- depth++;
- break;
- }
- }
- }
使用xml数据
- publicclassNetworkActivityextendsActivity{
- publicstaticfinalStringWIFI="Wi-Fi";
- publicstaticfinalStringANY="Any";
- privatestaticfinalStringURL="http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest";
- //WhetherthereisaWi-Ficonnection.
- privatestaticbooleanwifiConnected=false;
- //Whetherthereisamobileconnection.
- privatestaticbooleanmobileConnected=false;
- //Whetherthedisplayshouldberefreshed.
- publicstaticbooleanrefreshDisplay=true;
- publicstaticStringsPref=null;
- ...
- //UsesAsyncTasktodownloadtheXMLfeedfromstackoverflow.com.
- publicvoidloadPage(){
- if((sPref.equals(ANY))&&(wifiConnected||mobileConnected)){
- newDownloadXmlTask().execute(URL);
- }
- elseif((sPref.equals(WIFI))&&(wifiConnected)){
- newDownloadXmlTask().execute(URL);
- }else{
- //showerror
- }
- }
动态下载xml
- //ImplementationofAsyncTaskusedtodownloadXMLfeedfromstackoverflow.com.
- privateclassDownloadXmlTaskextendsAsyncTask<String,Void,String>{
- @Override
- protectedStringdoInBackground(String...urls){
- try{
- returnloadXmlFromNetwork(urls[0]);
- }catch(IOExceptione){
- returngetResources().getString(R.string.connection_error);
- }catch(XmlPullParserExceptione){
- returngetResources().getString(R.string.xml_error);
- }
- }
- @Override
- protectedvoidonPostExecute(Stringresult){
- setContentView(R.layout.main);
- //DisplaystheHTMLstringintheUIviaaWebView
- WebViewmyWebView=(WebView)findViewById(R.id.webview);
- myWebView.loadData(result,"text/html",null);
- }
- }
加载xml
- //UploadsXMLfromstackoverflow.com,parsesit,andcombinesitwith
- //HTMLmarkup.ReturnsHTMLstring.
- privateStringloadXmlFromNetwork(StringurlString)throwsXmlPullParserException,IOException{
- InputStreamstream=null;
- //Instantiatetheparser
- StackOverflowXmlParserstackOverflowXmlParser=newStackOverflowXmlParser();
- List<Entry>entries=null;
- Stringtitle=null;
- Stringurl=null;
- Stringsummary=null;
- CalendarrightNow=Calendar.getInstance();
- DateFormatformatter=newSimpleDateFormat("MMMddh:mmaa");
- //Checkswhethertheusersetthepreferencetoincludesummarytext
- SharedPreferencessharedPrefs=PreferenceManager.getDefaultSharedPreferences(this);
- booleanpref=sharedPrefs.getBoolean("summaryPref",false);
- StringBuilderhtmlString=newStringBuilder();
- htmlString.append("<h3>"+getResources().getString(R.string.page_title)+"</h3>");
- htmlString.append("<em>"+getResources().getString(R.string.updated)+""+
- formatter.format(rightNow.getTime())+"</em>");
- try{
- stream=downloadUrl(urlString);
- entries=stackOverflowXmlParser.parse(stream);
- //MakessurethattheInputStreamisclosedaftertheappis
- //finishedusingit.
- }finally{
- if(stream!=null){
- stream.close();
- }
- }
- //StackOverflowXmlParserreturnsaList(called"entries")ofEntryobjects.
- //EachEntryobjectrepresentsasinglepostintheXMLfeed.
- //ThissectionprocessestheentrieslisttocombineeachentrywithHTMLmarkup.
- //EachentryisdisplayedintheUIasalinkthatoptionallyincludes
- //atextsummary.
- for(Entryentry:entries){
- htmlString.append("<p><ahref='");
- htmlString.append(entry.link);
- htmlString.append("'>"+entry.title+"</a></p>");
- //Iftheusersetthepreferencethttp://oincludesummarytext,
- //addsittothedisplay.
- if(pref){
- htmlString.append(entry.summary);
- }
- }
- returnhtmlString.toString();
- }
- //GivenastringrepresentationofaURL,setsupaconnectionandgets
- //aninputstream.
- privateInputStreamdownloadUrl(StringurlString)throwsIOException{
- URLurl=newURL(urlString);
- HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
- conn.setReadTimeout(10000/*milliseconds*/);
- conn.setConnectTimeout(15000/*milliseconds*/);
- conn.setRequestMethod("GET");
- conn.setDoInput(true);
- //Startsthequery
- conn.connect();
- InputStreamstream=conn.getInputStream();
- }
上面的片段代码为了帮助大家理解,这里还是老习惯,贴上项目的源代码,本来是有项目截图,运行效果图之类的图的,只是这篇文章太长了,再弄图片,就更占篇幅了,所以就只贴源码了,大家可以自己的运行起来看看,希望能从整体的架构和具体的代码细节上帮助到大家。点击打开下载链接
相关推荐
在Android平台上进行网络操作是应用程序开发中的重要环节,它允许应用与远程服务器进行数据交互,如获取用户信息、上传下载文件、同步数据等。本文将深入探讨“Android网络操作”的核心概念、常用方法以及如何利用...
Android应用的主要交互发生在主线程,如果在这个线程执行耗时操作(如网络请求、数据库操作或大文件读写),会导致UI无响应,即著名的ANR(Application Not Responding)错误。为了解决这个问题,Android提供了线程...
如果需要执行网络相关的命令,则需要: ```xml <uses-permission android:name="android.permission.INTERNET"/> ``` 更复杂的情况下,可能需要动态请求权限,特别是在Android 6.0(API级别23)及以上版本。 对于...
Android 系统或 Android 应用执行 shell 脚本是 Android 开发中的一项重要技术,通过执行 shell 脚本,可以实现一些复杂的操作,例如设置网络参数、启动服务等。下面我们将介绍两种让 Android 系统或 Android 应用...
在Android开发中,网络操作是不可或缺的一部分,尤其是在构建与服务器交互的应用时。本Demo——"Android 网络类型及操作相关demo"专注于检测和管理设备的网络状态,特别是WiFi和移动数据。以下是对该Demo中涉及的...
在Android开发中,网络操作是不可或缺的一部分,它使得应用程序能够获取远程数据,实现与服务器的交互。本知识点主要聚焦于如何在Android中进行网络操作的封装,以便开发者能更便捷地进行网络请求,提高开发效率。 ...
此外,Android有严格的网络访问策略,比如在主线程中直接进行网络操作会导致ANR(Application Not Responding)错误,因此网络操作通常应在工作线程或者IntentService中执行。为了提高用户体验,还可以使用AsyncTask...
在Android 4.0及以上版本中,为了保证用户界面的流畅性,网络操作通常不会在主线程中执行,而是通过子线程或者异步任务来处理。以下是对这个主题的详细阐述: 1. **网络访问基础** - Android系统出于性能和用户...
由于Android主线程不支持耗时操作,网络请求和图片加载通常在后台线程执行,然后通过Handler、AsyncTask或者使用RxJava等异步编程库来更新UI。 总的来说,这个“android实战 网络图片浏览器”项目涵盖了Android开发...
4. **网络请求的异步处理**:Android主线程不应直接执行网络操作,否则可能导致应用无响应。可以使用AsyncTask、Handler/Looper或Retrofit(一个基于OkHttp的高级网络库)进行异步处理。 5. **HTTPS与安全**:对于...
你可以创建一个继承自AsyncTask的类,重写doInBackground()方法来执行网络请求,然后在onPostExecute()方法中更新UI。 2. **使用Volley库**:Volley是Google推荐的网络请求库,它内部实现了线程池和请求队列,能...
2. 运行应用:启动待调试的应用,执行网络操作。 3. 监控调试:在调试助手中查看捕获的网络请求,分析问题。 4. 修改与重发:若需要,可修改请求参数并重新发送,观察不同参数下的服务器响应。 5. 结束调试:完成...
系统运行库层是Android的核心组成部分,它包含了各种库和运行环境,如Dalvik虚拟机(现在已被ART取代)用于执行Java代码,以及C/C++库,如SQLite用于数据库管理,OpenSSL用于网络安全,以及多媒体框架等。...
在Android开发中,掌握文件操作、网络通信、数据库管理和用户界面(UI)设计是至关重要的。以下是对这些关键知识点的详细说明: 1. **文件操作**: - Android中的文件操作主要包括读取和写入。`File`类是进行文件...
// 网络连接可用,执行网络操作 } else { // 网络未连接,提示用户或执行相应操作 } ``` 5. **监听网络变化** 若要实时监听网络状态的变化,可以注册一个`BroadcastReceiver`来接收网络状态改变的广播: ```...
罗列了android常用两种检测网络方法,在工作线程中定时执行检测操作
3. **AsyncTask**:Android提供的轻量级异步任务类,用于在后台线程执行网络操作,避免阻塞UI线程。但因为生命周期问题,不建议在大型项目中广泛使用。 4. **Volley库**:Google推出的网络请求库,提供了高效的缓存...
这不仅可以帮助开发者判断应用程序是否应该执行某些依赖于网络的操作(例如数据同步、下载资源等),还可以根据当前网络环境为用户提供更合适的提示或界面显示。 #### 1. 导入必要的包 首先,为了实现检测网络状态...