- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在制作一个 android 应用程序,首先我将 XML 从网络保存到 sdcard 并成功保存,然后在尝试解析该 XML 但出现运行时错误后。我在下面添加了我的 LogCat。
这是我的代码,
protected String doInBackground(String... params)
{
// TODO Auto-generated method stub
try {
URL url = new URL(data);
stringList=new ArrayList<String>();
stringList1=new ArrayList<String>();
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
//set the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
File SDCardRoot = new File("/sdcard/");
//create a new file, specifying the path, and the filename
//which we want to save the file as.
File file = new File(SDCardRoot,"hello.xml");
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 )
{
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;
int progress=(int)(downloadedSize*100/totalSize);
//this is where you would do something to report the prgress, like this maybe
//updateProgress(downloadedSize, totalSize);
}
//close the output stream when done
fileOutput.close();
InputStream is = new FileInputStream(file.getPath());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(is));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("Table");
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("State");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
state=((Node) nameList.item(0)).getNodeValue();
Element fstElmnt1 = (Element) node;
NodeList nameList1 = fstElmnt1.getElementsByTagName("District");
Element nameElement1 = (Element) nameList1.item(0);
nameList1 = nameElement1.getChildNodes();
district=((Node) nameList1.item(0)).getNodeValue();
if(!stringList.contains(state) && !stringList1.contains(district))
{
stringList.add(state);
stringList1.add(district);
DatabaseHandler mydb = new DatabaseHandler(getApplicationContext());
// inserting new label into database
mydb.insertData(state,district);
//mydb.close();
}
//System.out.println("State : "+((Node) nameList.item(0)).getNodeValue());
}
loadStateSpinnerData();
loadDistrictSpinnerData();
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(), "No Internet Connection!", Toast.LENGTH_LONG).show();
}
catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SAXException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
还有我的 LogCat,
10-23 13:02:49.059: E/AndroidRuntime(8678): FATAL EXCEPTION: AsyncTask #1
10-23 13:02:49.059: E/AndroidRuntime(8678): java.lang.RuntimeException: An error occured while executing doInBackground()
10-23 13:02:49.059: E/AndroidRuntime(8678): at android.os.AsyncTask$3.done(AsyncTask.java:200)
10-23 13:02:49.059: E/AndroidRuntime(8678): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
10-23 13:02:49.059: E/AndroidRuntime(8678): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
10-23 13:02:49.059: E/AndroidRuntime(8678): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
10-23 13:02:49.059: E/AndroidRuntime(8678): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-23 13:02:49.059: E/AndroidRuntime(8678): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-23 13:02:49.059: E/AndroidRuntime(8678): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-23 13:02:49.059: E/AndroidRuntime(8678): at java.lang.Thread.run(Thread.java:1019)
10-23 13:02:49.059: E/AndroidRuntime(8678): Caused by: java.lang.ClassCastException: org.apache.harmony.xml.dom.TextImpl
10-23 13:02:49.059: E/AndroidRuntime(8678): at com.example.androidspinnerfromsqlite.AndroidSpinnerFromSQLiteActivity$XmlParsing.doInBackground(AndroidSpinnerFromSQLiteActivity.java:190)
10-23 13:02:49.059: E/AndroidRuntime(8678): at com.example.androidspinnerfromsqlite.AndroidSpinnerFromSQLiteActivity$XmlParsing.doInBackground(AndroidSpinnerFromSQLiteActivity.java:1)
10-23 13:02:49.059: E/AndroidRuntime(8678): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-23 13:02:49.059: E/AndroidRuntime(8678): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-23 13:02:49.059: E/AndroidRuntime(8678): ... 4 more
10-23 13:02:56.189: E/WindowManager(8678): Activity com.example.androidspinnerfromsqlite.AndroidSpinnerFromSQLiteActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405233a8 that was originally added here
10-23 13:02:56.189: E/WindowManager(8678): android.view.WindowLeaked: Activity com.example.androidspinnerfromsqlite.AndroidSpinnerFromSQLiteActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405233a8 that was originally added here
10-23 13:02:56.189: E/WindowManager(8678): at android.view.ViewRoot.<init>(ViewRoot.java:263)
10-23 13:02:56.189: E/WindowManager(8678): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
10-23 13:02:56.189: E/WindowManager(8678): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-23 13:02:56.189: E/WindowManager(8678): at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-23 13:02:56.189: E/WindowManager(8678): at android.app.Dialog.show(Dialog.java:241)
10-23 13:02:56.189: E/WindowManager(8678): at android.app.ProgressDialog.show(ProgressDialog.java:107)
10-23 13:02:56.189: E/WindowManager(8678): at android.app.ProgressDialog.show(ProgressDialog.java:90)
10-23 13:02:56.189: E/WindowManager(8678): at com.example.androidspinnerfromsqlite.AndroidSpinnerFromSQLiteActivity$XmlParsing.onPreExecute(AndroidSpinnerFromSQLiteActivity.java:78)
10-23 13:02:56.189: E/WindowManager(8678): at android.os.AsyncTask.execute(AsyncTask.java:391)
10-23 13:02:56.189: E/WindowManager(8678): at com.example.androidspinnerfromsqlite.AndroidSpinnerFromSQLiteActivity.onCreate(AndroidSpinnerFromSQLiteActivity.java:57)
10-23 13:02:56.189: E/WindowManager(8678): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-23 13:02:56.189: E/WindowManager(8678): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-23 13:02:56.189: E/WindowManager(8678): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-23 13:02:56.189: E/WindowManager(8678): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-23 13:02:56.189: E/WindowManager(8678): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-23 13:02:56.189: E/WindowManager(8678): at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 13:02:56.189: E/WindowManager(8678): at android.os.Looper.loop(Looper.java:130)
10-23 13:02:56.189: E/WindowManager(8678): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-23 13:02:56.189: E/WindowManager(8678): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 13:02:56.189: E/WindowManager(8678): at java.lang.reflect.Method.invoke(Method.java:507)
10-23 13:02:56.189: E/WindowManager(8678): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-23 13:02:56.189: E/WindowManager(8678): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-23 13:02:56.189: E/WindowManager(8678): at dalvik.system.NativeStart.main(Native Method)
不知道到底哪里出错了。
请帮忙。
谢谢。
最佳答案
试试这个:
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
loadStateSpinnerData();
loadDistrictSpinnerData();
}
});
改变这个:
if(!stringList.contains(state) && !stringList1.contains(district))
{
stringList.add(state);
stringList1.add(district);
DatabaseHandler mydb = new DatabaseHandler(getApplicationContext());
// inserting new label into database
mydb.insertData(state,district);
//mydb.close();
}
到
if(!stringList.contains(state) || !stringList1.contains(district))
{
stringList.add(state);
stringList1.add(district);
DatabaseHandler mydb = new DatabaseHandler(getApplicationContext());
// inserting new label into database
mydb.insertData(state,district);
//mydb.close();
}
关于android - 从 XML 解析数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19535813/
我一直在使用 AJAX 从我正在创建的网络服务中解析 JSON 数组时遇到问题。我的前端是一个简单的 ajax 和 jquery 组合,用于显示从我正在创建的网络服务返回的结果。 尽管知道我的数据库查
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我在尝试运行 Android 应用程序时遇到问题并收到以下错误 java.lang.NoClassDefFoundError: com.parse.Parse 当我尝试运行该应用时。 最佳答案 在这
有什么办法可以防止etree在解析HTML内容时解析HTML实体吗? html = etree.HTML('&') html.find('.//body').text 这给了我 '&' 但我想
我有一个有点疯狂的例子,但对于那些 JavaScript 函数作用域专家来说,它看起来是一个很好的练习: (function (global) { // our module number one
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我需要编写一个脚本来获取链接并解析链接页面的 HTML 以提取标题和其他一些数据,例如可能是简短的描述,就像您链接到 Facebook 上的内容一样。 当用户向站点添加链接时将调用它,因此在客户端启动
在 VS Code 中本地开发时,包解析为 C:/Users//AppData/Local/Microsoft/TypeScript/3.5/node_modules/@types//index而不是
我在将 json 从 php 解析为 javascript 时遇到问题 这是我的示例代码: //function MethodAjax = function (wsFile, param) {
我在将 json 从 php 解析为 javascript 时遇到问题 这是我的示例代码: //function MethodAjax = function (wsFile, param) {
我被赋予了将一种语言“翻译”成另一种语言的工作。对于使用正则表达式的简单逐行方法来说,源代码过于灵活(复杂)。我在哪里可以了解更多关于词法分析和解析器的信息? 最佳答案 如果你想对这个主题产生“情绪化
您好,我在解析此文本时遇到问题 { { { {[system1];1;1;0.612509325}; {[system2];1;
我正在为 adobe after effects 在 extendscript 中编写一些代码,最终变成了 javascript。 我有一个数组,我想只搜索单词“assemble”并返回整个 jc3_
我有这段代码: $(document).ready(function() { // }); 问题:FB_RequireFeatures block 外部的代码先于其内部的代码执行。因此 who
背景: netcore项目中有些服务是在通过中间件来通信的,比如orleans组件。它里面服务和客户端会指定网关和端口,我们只需要开放客户端给外界,服务端关闭端口。相当于去掉host,这样省掉了些
1.首先贴上我试验成功的代码 复制代码 代码如下: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
什么是 XML? XML 指可扩展标记语言(eXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言。 你可以通过本站学习 X
【PHP代码】 复制代码 代码如下: $stmt = mssql_init('P__Global_Test', $conn) or die("initialize sto
在SQL查询分析器执行以下代码就可以了。 复制代码代码如下: declare @t varchar(255),@c varchar(255) declare table_cursor curs
前言 最近练习了一些前端算法题,现在做个总结,以下题目都是个人写法,并不是标准答案,如有错误欢迎指出,有对某道题有新的想法的友友也可以在评论区发表想法,互相学习🤭 题目 题目一: 二维数组中的
我是一名优秀的程序员,十分优秀!