本文介绍了我的XML解析器(SAX解析器)解析URI唯一来源。我怎么能分析它存在于我的SAXParser的项目中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code只能解析URI的来源。

This is the code which can only parse URI sources.

    package com.example.xmlparserdeneme;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {

    ArrayList<String> titles = new ArrayList<String>();
    Context ctx = this;
    CustomAdapter adapter = null;
    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ctx = this;
        new parseXML().execute();

        Log.i("info2", titles.toString());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    class parseXML extends AsyncTask<String, Void, String> {

        MySaxHandler handler;

        @Override
        protected String doInBackground(String... params) {

            // ((Activity) ctx).setContentView(R.layout.activity_main);

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser parser = null;
            try {
                parser = spf.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }

            handler = new MySaxHandler();

            try {

                parser.parse("http://sosyalmedya.co/mobile-feed/", handler);
                // parser.parse("file:///android_asset/DirenisDB.txt", handler);
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            Log.i("info", titles.toString());

            // Toast.makeText(ctx, "POST", Toast.LENGTH_LONG).show();
            // setAdapter();

            return "a";

        }

        @Override
        protected void onPostExecute(String result) {

            lv = (ListView) findViewById(R.id.listView);
            lv.setAdapter(new CustomAdapter(titles, ctx));
        }

        public ArrayList<String> getArray() {
            return titles;
        }

        public class MySaxHandler extends DefaultHandler {

            StringBuffer chars;

            public void startElement(String uri, String localName,
                    String qName, Attributes atts) {
                chars = new StringBuffer();

            }

            public void endElement(String uri, String localName, String qName)
                    throws SAXException {

                if (localName.equalsIgnoreCase("key"))
                    titles.add(chars.toString());

            }

            public void characters(char ch[], int start, int length) {
                chars.append(new String(ch, start, length));
            }

        }

    }
}

我已经花了它近6个小时,但我仍然无法弄清楚。
我只是希望它来解析这个(文件)
                 parser.parse(文件:///android_asset/DirenisDB.txt,处理程序);

     而不是这个(URI)
     parser.parse(http://sosyalmedya.co/mobile-feed/,处理程序);

我会AP preciated如果你能解决我的问题。

I'd be appreciated if you can solve my problem.

推荐答案

json的code这是会被解析必须在布局/ RES /原始文件夹中。
这就是code键打开资源

the json code which is going to be parsed must be in the layout/res/Raw folder.and this is the code to open the resource.

db = parse(getResources().openRawResource(R.raw.database));

这篇关于我的XML解析器(SAX解析器)解析URI唯一来源。我怎么能分析它存在于我的SAXParser的项目中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 23:54