-3
URL url = new URL("https://www.setindia.com"); 
URLConnection urlConnectionObject = url.openConnection();
xmlHandlerObject = new XMLHandler(); 
xmlReaderObject.setContentHandler(xmlHandlerObject); 
xmlReaderObject.parse(new InputSource(urlConnectionObject.getInputStream())); 

// 最后一行的错误请给我一个解释(android 新手),这是正确的

主要活动:

    package com.example.testparser;

    import java.net.URL;
    import java.net.URLConnection;
    import java.security.KeyStore;

    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.KeyManagerFactory;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;

    import org.xml.sax.InputSource;
    import org.xml.sax.XMLReader;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends Activity {

    public static String TAG = "MYParser";

    GettersSetters getData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d(TAG, "OnCreate");

        View layout = findViewById(R.id.layout);

        TextView tittle[];
        TextView country[];
        XMLHandler xmlHandlerObject = null;

        try{
            Log.d(TAG, "try");
            SAXParserFactory saxParserFactoryObject = SAXParserFactory.newInstance();   //obtain and configure a SAX based parser
            SAXParser saxParserObject = saxParserFactoryObject.newSAXParser(); //obtaining object for SAX parser
            XMLReader xmlReaderObject = saxParserObject.getXMLReader();


            URL url = new URL("https://www.setindia.com/setindia_api/episode/1?date=22-10-2013&hd=1");
            URLConnection urlConnectionObject = url.openConnection();


            xmlHandlerObject = new XMLHandler();
            xmlReaderObject.setContentHandler(xmlHandlerObject);

            Log.d(TAG, "about to get an error");

            xmlReaderObject.parse(new InputSource(urlConnectionObject.getInputStream()));
            Log.d(TAG, "try end");  
    }
         catch (Exception e) {
               Log.e(TAG, e.getMessage());
        }
        Log.d(TAG, "OnCreate fetching the data from the xml");
        getData = xmlHandlerObject.getXMLData();

        tittle = new TextView[getData.getTittle().size()];
        country = new TextView[getData.getCountry().size()];

        for (int i = 0; i < getData.getTittle().size(); i++) {

            tittle[i] = new TextView(this);
            tittle[i].setText("ITEM is : " + getData.getTittle().get(i));

            country[i] = new TextView(this);
            country[i].setText("Country is :" + getData.getTittle().get(i));

            ((ViewGroup) layout).addView(tittle[i]);
            ((ViewGroup) layout).addView(country[i]);
             setContentView(R.layout.main);
        }

    }
    @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;
    }

}

吸气剂设置器:

package com.example.testparser;

import java.lang.reflect.Array;
import java.util.ArrayList;

import android.util.Log;

public class GettersSetters {
    private ArrayList<String> tittle = new ArrayList<String>();
    private ArrayList<String> country = new ArrayList<String>();

    public ArrayList<String> getCountry(){
        return country;
    }
    public ArrayList<String> getTittle(){

        return tittle;
    }
    public void setCountry(String country){
        this.country.add(country);
        Log.i("This is the country : ",country);

    }
     public void setTittle(String tittle){

         this.tittle.add(tittle);
         Log.i("This is the tittle : ",tittle);
     }

}

XML处理程序:

package com.example.testparser;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;


public class XMLHandler extends DefaultHandler {

    public static String TAG = "MYParser";
    public static GettersSetters data = null;
    String elementValue = null ;
    Boolean elementOn = false;

    public GettersSetters getXMLData(){
        Log.e(TAG, "getXMLdata -> "+data.toString());
        return data;
    }
    public void setXMLData(GettersSetters data){

        XMLHandler.data = data;
        Log.e(TAG, "setXmldata");
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        super.startElement(uri, localName, qName, attributes);
        elementOn = true;
        Log.d(TAG, "Checking the first element"+localName.equalsIgnoreCase("item"));
        if(localName.equalsIgnoreCase("item"))
        {
            Log.d(TAG, "Creating a new getter setter instance");
            data = new GettersSetters();            
        }
    }
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        super.characters(ch, start, length);
        if(elementOn)
        {
            elementValue = new String(ch,start,length);
            elementOn =false;
        }
    }
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        elementOn = false;
        super.endElement(uri, localName, qName);
        if(localName.equalsIgnoreCase("country"))
            data.setCountry(elementValue);
        if(localName.equalsIgnoreCase("tittle"))
            data.setTittle(elementValue);
    }
}

MAIN.Xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="SAGAR HERE"
        android:textSize="20dp"
        android:gravity="center_horizontal"
        android:id="@+id/layout"
        />

</LinearLayout>
4

1 回答 1

1

不要在 UI Thread (main) 上进行网络操作。您可以使用AsyncTask它来正确和轻松地使用 UI 线程。它允许在 UI 线程上执行后台操作并发布结果,而无需操作线程和/或处理程序(它使用内部线程池)。在以下位置查看更多信息:AsyncTask API

于 2013-10-29T11:26:55.397 回答