应用无法读取我的XML Web服务

时间:2014-06-26 05:19:33

标签: android xml web-services

我在www.ravaana.ml/webservice/1.xml中获得了一个xml 我在这里得到了另一个xml http://www.ravaana.ml/webservice/2.xml 我的应用程序无法读取它。我不知道该怎么做。

以下是代码:

公共类BeritaTerbaru扩展了Fragment {

// All variables
XMLParser parser;
Document doc;
String xml;
ListView lv;
ListViewAdapter adapter;
ArrayList<HashMap<String, String>> menuItems;
ProgressDialog pDialog;

// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_JUDUL = "judul";
static final String KEY_BERITA = "berita";

// Flag for current page
int current_page = 0;

public void onStart() {
    super.onStart();
    lv = (ListView) getView().findViewById(R.id.list);

    menuItems = new ArrayList<HashMap<String, String>>();

    new loadMoreListView().execute();

    // LoadMore button
    Button btnLoadMore = new Button(getActivity());
    btnLoadMore.setText("Load More");

    // Adding Load More button to lisview at bottom
    lv.addFooterView(btnLoadMore);

    // Getting adapter
    adapter = new ListViewAdapter(getActivity(), menuItems);
    lv.setAdapter(adapter);

    /**
     * Listening to Load More button click event
     * */
    btnLoadMore.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Starting a new async task
            new loadMoreListView().execute();
        }
    });

    /**
     * Listening to listview single row selected
     * **/
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name))
                    .getText().toString();

            // Starting new intent
            Intent in = new Intent(getActivity(),
                    SingleMenuItemActivity.class);
            in.putExtra(KEY_JUDUL, name);
            startActivity(in);
        }
    });
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_beritaterbaru,
            container, false);
    return view;
}

/**
 * Async Task that send a request to url Gets new list view data Appends to
 * list view
 * */
private class loadMoreListView extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // Showing progress dialog before sending http request
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Please wait..");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected Void doInBackground(Void... unused) {
        // increment current page
        current_page += 1;

        // Next page request
        String URL = "http://www.ravaana.ml/webservice/" + current_page
                + ".xml";
        parser = new XMLParser();
        xml = parser.getXmlFromUrl(URL); // getting XML
        doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_ITEM);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);

            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_JUDUL, parser.getValue(e, KEY_JUDUL));
            map.put(KEY_BERITA, parser.getValue(e, KEY_BERITA));
            // adding HashList to ArrayList
            menuItems.add(map);
        }
        return null;
    }

    protected void onPostExecute(Void unused) {
        // closing progress dialog
        pDialog.dismiss();

        // get listview current position - used to maintain scroll position
        int currentPosition = lv.getFirstVisiblePosition();

        // Appending new data to menuItems ArrayList
        adapter = new ListViewAdapter(getActivity(), menuItems);
        lv.setAdapter(adapter);

        // Setting new scroll position
        lv.setSelectionFromTop(currentPosition + 0, 0);
    }
}

}

这是logcat

06-26 00:57:30.050: D/dalvikvm(808): GC_FOR_ALLOC freed 206K, 6% free 5068K/5372K, paused 32ms, total 32ms
06-26 00:57:30.060: E/Error:(808): Unexpected token (position:TEXT @1:4 in java.io.StringReader@b4fbbbf0) 
06-26 00:57:30.060: W/dalvikvm(808): threadid=11: thread exiting with uncaught exception (group=0xb4aa7b90)
06-26 00:57:30.060: E/AndroidRuntime(808): FATAL EXCEPTION: AsyncTask #1
06-26 00:57:30.060: E/AndroidRuntime(808): Process: info.androidhive.slidingmenu, PID: 808
06-26 00:57:30.060: E/AndroidRuntime(808): java.lang.RuntimeException: An error occured while executing doInBackground()
06-26 00:57:30.060: E/AndroidRuntime(808):  at android.os.AsyncTask$3.done(AsyncTask.java:300)
06-26 00:57:30.060: E/AndroidRuntime(808):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
06-26 00:57:30.060: E/AndroidRuntime(808):  at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
06-26 00:57:30.060: E/AndroidRuntime(808):  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
06-26 00:57:30.060: E/AndroidRuntime(808):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-26 00:57:30.060: E/AndroidRuntime(808):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-26 00:57:30.060: E/AndroidRuntime(808):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-26 00:57:30.060: E/AndroidRuntime(808):  at java.lang.Thread.run(Thread.java:841)
06-26 00:57:30.060: E/AndroidRuntime(808): Caused by: java.lang.NullPointerException
06-26 00:57:30.060: E/AndroidRuntime(808):  at info.androidhive.slidingmenu.BeritaTerbaru$loadMoreListView.doInBackground(BeritaTerbaru.java:129)
06-26 00:57:30.060: E/AndroidRuntime(808):  at info.androidhive.slidingmenu.BeritaTerbaru$loadMoreListView.doInBackground(BeritaTerbaru.java:1)
06-26 00:57:30.060: E/AndroidRuntime(808):  at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-26 00:57:30.060: E/AndroidRuntime(808):  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-26 00:57:30.060: E/AndroidRuntime(808):  ... 4 more
06-26 00:57:32.400: E/WindowManager(808): android.view.WindowLeaked: Activity info.androidhive.slidingmenu.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b4f5d9e0 V.E..... R......D 0,0-383,96} that was originally added here
06-26 00:57:32.400: E/WindowManager(808):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:346)
06-26 00:57:32.400: E/WindowManager(808):   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
06-26 00:57:32.400: E/WindowManager(808):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
06-26 00:57:32.400: E/WindowManager(808):   at android.app.Dialog.show(Dialog.java:286)
06-26 00:57:32.400: E/WindowManager(808):   at info.androidhive.slidingmenu.BeritaTerbaru$loadMoreListView.onPreExecute(BeritaTerbaru.java:116)
06-26 00:57:32.400: E/WindowManager(808):   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
06-26 00:57:32.400: E/WindowManager(808):   at android.os.AsyncTask.execute(AsyncTask.java:535)
06-26 00:57:32.400: E/WindowManager(808):   at info.androidhive.slidingmenu.BeritaTerbaru.onStart(BeritaTerbaru.java:48)
06-26 00:57:32.400: E/WindowManager(808):   at android.app.Fragment.performStart(Fragment.java:1724)
06-26 00:57:32.400: E/WindowManager(808):   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:918)
06-26 00:57:32.400: E/WindowManager(808):   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
06-26 00:57:32.400: E/WindowManager(808):   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
06-26 00:57:32.400: E/WindowManager(808):   at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1858)
06-26 00:57:32.400: E/WindowManager(808):   at android.app.Activity.performStart(Activity.java:5259)
06-26 00:57:32.400: E/WindowManager(808):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2149)
06-26 00:57:32.400: E/WindowManager(808):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
06-26 00:57:32.400: E/WindowManager(808):   at android.app.ActivityThread.access$700(ActivityThread.java:135)
06-26 00:57:32.400: E/WindowManager(808):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
06-26 00:57:32.400: E/WindowManager(808):   at android.os.Handler.dispatchMessage(Handler.java:102)
06-26 00:57:32.400: E/WindowManager(808):   at android.os.Looper.loop(Looper.java:137)
06-26 00:57:32.400: E/WindowManager(808):   at android.app.ActivityThread.main(ActivityThread.java:4998)
06-26 00:57:32.400: E/WindowManager(808):   at java.lang.reflect.Method.invokeNative(Native Method)
06-26 00:57:32.400: E/WindowManager(808):   at java.lang.reflect.Method.invoke(Method.java:515)
06-26 00:57:32.400: E/WindowManager(808):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
06-26 00:57:32.400: E/WindowManager(808):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
06-26 00:57:32.400: E/WindowManager(808):   at dalvik.system.NativeStart.main(Native Method)
06-26 00:57:32.560: I/Process(808): Sending signal. PID: 808 SIG: 9

我已经包含了所需的所有课程,但仍无法运行它。

1 个答案:

答案 0 :(得分:0)

试试这个..

protected Void doInBackground(Void... unused) {
    current_page += 1;

    try {
    String urls = "http://www.ravaana.ml/webservice/" + current_page + ".xml";

    URL url = new URL(urls);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(url.openStream()));

    doc.getDocumentElement().normalize();

    NodeList nodeList = doc.getElementsByTagName(KEY_ITEM);

    for (int i = 0; i < nodeList.getLength(); i++) {

    HashMap<String, String> map = new HashMap<String, String>();
    Node node = nodeList.item(i);       

    Element fstElmnt = (Element) node;
    NodeList nameList = fstElmnt.getElementsByTagName(KEY_ID);
    Element nameElement = (Element) nameList.item(0);
    nameList = nameElement.getChildNodes();

    map.put(KEY_ID, (((Node) nameList.item(0)).getNodeValue()));

    Element fstElmnt1 = (Element) node;
    NodeList nameList1 = fstElmnt1.getElementsByTagName(KEY_JUDUL);
    Element nameElement1 = (Element) nameList1.item(0);
    nameList1 = nameElement1.getChildNodes();

    map.put(KEY_JUDUL, (((Node) nameList1.item(0)).getNodeValue()));

    Element fstElmnt2 = (Element) node;
    NodeList nameList2 = fstElmnt2.getElementsByTagName(KEY_BERITA);
    Element nameElement2 = (Element) nameList2.item(0);
    nameList2 = nameElement2.getChildNodes();

    map.put(KEY_BERITA, (((Node) nameElement2.item(0)).getNodeValue()));

    menuItems.add(map);

    }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}