Java Android XML读取器出错
我有一个可以正常工作的XML解析器,但是如果用户连接到一个没有浏览应用程序的网络,该解析器就会中断。要放一个功能来检测用户是否连接,但是上面提到的问题,用户会连接但没有浏览 我的代码如下:Java Android XML读取器出错,java,android,xml,xml-parsing,Java,Android,Xml,Xml Parsing,我有一个可以正常工作的XML解析器,但是如果用户连接到一个没有浏览应用程序的网络,该解析器就会中断。要放一个功能来检测用户是否连接,但是上面提到的问题,用户会连接但没有浏览 我的代码如下: public void chamaParser(){ Boolean conected = Conectado(this); if(conected == true){ new DoInBackground().execute(); } else { m
public void chamaParser(){
Boolean conected = Conectado(this);
if(conected == true){
new DoInBackground().execute();
} else {
mPullRefreshListViewRSS.onRefreshComplete();
new AlertDialog.Builder(PullToRefreshListActivityRSS.this)
.setTitle("Alerta!")
.setMessage("Não foi possível estabelecer uma conexão com a Internet, verifique sua rede ou tente novamente mais tarde.")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {}
}).show();
}
}
public void do_update() {
RssParser.parse();
}
public static boolean Conectado(Context context) {
try {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
String LogSync = null;
String LogToUserTitle = null;
if (cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected()) {
LogSync += "\nConectado a Internet 3G ";
LogToUserTitle += "Conectado a Internet 3G ";
return true;
} else if(cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
LogSync += "\nConectado a Internet WIFI ";
LogToUserTitle += "Conectado a Internet WIFI ";
return true;
} else {
LogSync += "\nNão possui conexão com a internet ";
LogToUserTitle += "Não possui conexão com a internet ";
return false;
}
} catch (Exception e) {
return false;
}
}
private class DoInBackground extends AsyncTask<Void, Void, Void> implements DialogInterface.OnCancelListener {
private ProgressDialog dialog;
protected void onPreExecute() {
//dialog = ProgressDialog.show(PullToRefreshListActivityRSS.this, "", "Carregando...", true);
dialog = ProgressDialog.show(PullToRefreshListActivityRSS.this, "", "Carregando...", true, true, new DialogInterface.OnCancelListener(){
@Override
public void onCancel(DialogInterface dialog) {
mPullRefreshListViewRSS.onRefreshComplete();
DoInBackground.this.cancel(true);
// finish();
}
});
}
protected Void doInBackground(Void... unused) {
PodcastAdapterRSS.topBarRSS=true;
do_update();
return null;
}
public void chamaParser(){
布尔连接=连接(this);
如果(连接==真){
新建DoInBackground().execute();
}否则{
mPullRefreshListViewRSS.onRefreshComplete();
新建AlertDialog.Builder(PullToRefreshListActivityRSS.this)
.setTitle(“Alerta!”)
.setMessage(“在互联网上,验证新技术是否有效。”)
.setNeutralButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){}
}).show();
}
}
public void do_update(){
RssParser.parse();
}
公共静态布尔连接(上下文){
试一试{
ConnectivityManager cm=(ConnectivityManager)context.getSystemService(context.CONNECTIVITY_服务);
字符串LogSync=null;
字符串LogToUserTitle=null;
if(cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE.isConnected()){
LogSync+=“\n连接互联网3G”;
LogToUserTitle+=“连接互联网3G”;
返回true;
}else if(cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI.isConnected()){
LogSync+=“\n连接互联网WIFI”;
LogToUserTitle+=“连接互联网WIFI”;
返回true;
}否则{
LogSync+=“\nNão possui conexão com a internet”;
LogToUserTitle+=“不可能使用互联网”;
返回false;
}
}捕获(例外e){
返回false;
}
}
私有类DoInBackground扩展AsyncTask实现DialogInterface.OnCancelListener{
私人对话;
受保护的void onPreExecute(){
//dialog=ProgressDialog.show(PullToRefreshListActivityRSS.this,“,“Carregando…”,true);
dialog=ProgressDialog.show(PullToRefreshListActivityRSS.this,“,”Carregando…”,true,true,new DialogInterface.OnCancelListener(){
@凌驾
public void onCancel(对话框接口对话框){
mPullRefreshListViewRSS.onRefreshComplete();
DoInBackground.this.cancel(true);
//完成();
}
});
}
受保护的Void doInBackground(Void…未使用){
PodcastAdapterRSS.topBarRSS=true;
do_update();
返回null;
}
解析器XML:
public class RssParser {
public static void parse() {
URL url;
try {
url = new URL("http://www.xxxx.com.br/feed/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc;
doc = db.parse(url.openStream());
doc.getDocumentElement().normalize();
NodeList itemLst = doc.getElementsByTagName("item");
arrays.Title = new String[itemLst.getLength()];
arrays.Link = new String[itemLst.getLength()];
arrays.Description = new String[itemLst.getLength()];
// arrays.PodcastMedia = new String[itemLst.getLength()];
for(int i=0; i < itemLst.getLength(); i++){
Node item = itemLst.item(i);
if(item.getNodeType() == Node.ELEMENT_NODE){
Element ielem = (Element)item;
NodeList title = ielem.getElementsByTagName("title");
NodeList link = ielem.getElementsByTagName("link");
//NodeList description = ielem.getElementsByTagName("description");
NodeList content = ielem.getElementsByTagName("description");
// NodeList media = ielem.getElementsByTagName("media:content");
// String mediaurl = media.item(0).getAttributes().getNamedItem("url").getNodeValue();
arrays.Title[i] = title.item(0).getChildNodes().item(0).getNodeValue();
arrays.Link[i] = link.item(0).getChildNodes().item(0).getNodeValue();
arrays.Description[i] = content.item(0).getChildNodes().item(0).getNodeValue();
}
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
公共类RssParser{
公共静态void parse(){
网址;
试一试{
url=新url(“http://www.xxxx.com.br/feed/");
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
if(conn.getResponseCode()==HttpURLConnection.HTTP\u确定){
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
文件文件;
doc=db.parse(url.openStream());
doc.getDocumentElement().normalize();
nodelistitemlst=doc.getElementsByTagName(“项”);
arrays.Title=新字符串[itemLst.getLength()];
arrays.Link=新字符串[itemLst.getLength()];
arrays.Description=新字符串[itemLst.getLength()];
//arrays.PodcastMedia=新字符串[itemLst.getLength()];
对于(int i=0;i
日志类别:
07-30 15:20:48.319:带系统错误(10997):
java.net.UnknownHostException:www.xxxx.com.br 07-30
15:20:48.319:W/System.err(10997):在
java.net.InetAddress.lookupHostByName(InetAddress.java:508)07-30
15:20:48.319:W/System.err(10997):在
java.net.InetAddress.getAllByNameImpl(InetAddress.java:296)07-30
15:20:48.319:W/System.err(10997):在
java.net.InetAddress.getAllByName(InetAddress.java:258)07-30
15:20:48.319:W/System.err(10997):在
org.apache.harmony.luni.inte
<uses-permission android:name="android.permission.INTERNET" />