解析XML数据形成的Internet2
你好,大家好我开发一个简单的应用程序,通过使用SAX读取来自互联网一个XML文件,但我得到的logcat此错误的任何帮助,可帮助解析XML数据形成的Internet2
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.reaf.xml"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.read.xml.DataHandler"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" >
</activity>
</application>
</manifest>
logcat的
10-06 08:29:52.204: D/AndroidRuntime(614): Shutting down VM
10-06 08:29:52.204: W/dalvikvm(614): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-06 08:29:52.254: E/AndroidRuntime(614): FATAL EXCEPTION: main
10-06 08:29:52.254: E/AndroidRuntime(614): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.read.xml/com.read.xml.DataHandler}: java.lang.ClassCastException: com.read.xml.DataHandler
10-06 08:29:52.254: E/AndroidRuntime(614): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
10-06 08:29:52.254: E/AndroidRuntime(614): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-06 08:29:52.254: E/AndroidRuntime(614): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-06 08:29:52.254: E/AndroidRuntime(614): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-06 08:29:52.254: E/AndroidRuntime(614): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 08:29:52.254: E/AndroidRuntime(614): at android.os.Looper.loop(Looper.java:123)
10-06 08:29:52.254: E/AndroidRuntime(614): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-06 08:29:52.254: E/AndroidRuntime(614): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 08:29:52.254: E/AndroidRuntime(614): at java.lang.reflect.Method.invoke(Method.java:507)
10-06 08:29:52.254: E/AndroidRuntime(614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-06 08:29:52.254: E/AndroidRuntime(614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
8月10日至6日:29:52.254:E/AndroidRuntime(614):在dalvik.system.NativeStart.main(本机方法) 8月10日至6日:29:52.254:E/AndroidRuntime(614):引起:java.lang.ClassCastException:com.read.xml.DataHandler 10-06 08:29:52.254:E/AndroidRuntime(614):在android.app.Instrumentation.newActivity(Instrumentation.java:1021) 10-06 08:29:52.254:E/AndroidRuntime(614):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java :1561) 10-06 08:29:52.254:E/AndroidRuntime(614):... 11更多 10-06 08:29:54.514:I /进程(614):发送信号。 PID:614 SIG:9
,这是的DataHandler类
package com.read.xml;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.widget.TextView;
public class DataHandler extends DefaultHandler {
//list for imported product data
private ArrayList<TextView> theViews;
//string to track each entry
private String currBrand = "";
//flag to keep track of XML processing
private boolean isProduct = false;
//context for user interface
private Context theContext;
//constructor
public DataHandler(Context cont) {
super();
theViews = new ArrayList<TextView>();
theContext = cont;
}
//start of the XML document
public void startDocument()
{ Log.i("DataHandler", "Start of XML document"); }
//end of the XML document
public void endDocument()
{ Log.i("DataHandler", "End of XML document"); }
//opening element tag
public void startElement (String uri, String name, String qName, Attributes atts)
{
//find out if the element is a brand
if(qName.equals("brand"))
{
//set product tag to false
isProduct = false;
//create View item for brand display
TextView brandView = new TextView(theContext);
brandView.setTextColor(Color.rgb(73, 136, 83));
//add the attribute value to the displayed text
String viewText = "Items from " + atts.getValue("name") + ":";
brandView.setText(viewText);
//add the new view to the list
theViews.add(brandView);
}
//the element is a product
else if(qName.equals("product"))
isProduct = true;
}
//closing element tag
public void endElement (String uri, String name, String qName)
{
if(qName.equals("brand"))
{
//create a View item for the products
TextView productView = new TextView(theContext);
productView.setTextColor(Color.rgb(192, 199, 95));
//display the compiled items
productView.setText(currBrand);
//add to the list
theViews.add(productView);
//reset the variable for future items
currBrand = "";
}
}
//element content
public void characters (char ch[], int start, int length)
{
//string to store the character content
String currText = "";
//loop through the character array
for (int i=start; i<start+length; i++)
{
switch (ch[i]) {
case '\\':
break;
case '"':
break;
case '\n':
break;
case '\r':
break;
case '\t':
break;
default:
currText += ch[i];
break;
}
}
//prepare for the next item
if(isProduct && currText.length()>0)
currBrand += currText+"\n";
}
public ArrayList<TextView> getData()
{
//take care of SAX, input and parsing errors
try
{
//set the parsing driver
System.setProperty("org.xml.sax.driver","org.xmlpull.v1.sax2.Driver");
//create a parser
SAXParserFactory parseFactory = SAXParserFactory.newInstance();
SAXParser xmlParser = parseFactory.newSAXParser();
//get an XML reader
XMLReader xmlIn = xmlParser.getXMLReader();
//instruct the app to use this object as the handler
xmlIn.setContentHandler(this);
//provide the name and location of the XML file
URL xmlURL = new URL("www.data-online.bugs3.com/public_html/test.xml");
//open the connection and get an input stream
URLConnection xmlConn = xmlURL.openConnection();
InputStreamReader xmlStream = new
InputStreamReader(xmlConn.getInputStream());
//build a buffered reader
BufferedReader xmlBuff = new BufferedReader(xmlStream);
//parse the data
xmlIn.parse(new InputSource(xmlBuff));
}
catch(SAXException se) { Log.e("AndroidTestsActivity",
"SAX Error " + se.getMessage()); }
catch(IOException ie) { Log.e("AndroidTestsActivity",
"Input Error " + ie.getMessage()); }
catch(Exception oe) { Log.e("AndroidTestsActivity",
"Unspecified Error " + oe.getMessage()); }
//return the parsed product list
return theViews;
}
}
我不知道什么是错误
请帮助
你在哪里打电话DataHandler.getData?如果这是在一个活动构造函数或创建调用中,这将导致NetworkOnMainThreadException。
如果是这样,请考虑的AsyncTask的情况:http://developer.android.com/reference/android/os/AsyncTask.html
您需要调用的getData在doInBackground。
yest它是在主要方法oncreate方法,所以我怎么能解决这个错误 – jiji
你不能在主线程上做这样的网络操作和URL.openConnection,所以你需要使用AsyncTask。 –
这个答案是一个很好的例子:http://*.com/a/9671602/511012 –
您没有空的构造函数集集。你应该有一个空的构造函数来使用newInstance方法,就像我在前面的问题中提到的那样。类似于
public DataHandler() {
}
好吧我做了你说的和关于构造函数的错误消失了,谢谢你,但大厅其他错误呢? – jiji
重复这个http://*.com/questions/12757873/parsing-xml-data-form-the-internet ??? – sakthisundar
是的我那个问题是我的,但我显示这个问题后,我添加到清单文件的活动,但仍然得到相同的错误 – jiji