Android Studio: RSS reader with xml

I am trying to create an RSS reader from an xml file.

Most of my code is built off of this template: http://www.tutorialspoint.com/android/android_rss_reader.htm

I modified the code for my overall goal. The goal is to create a RSS feed with in a web based mobile application.

The problem is the app can not pull the rss feed properly. I get a statement saying your url can not be found.

Any help is much appreciated.

Activity Main

<TextView android:text="RSS example" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textview"
    android:textSize="35dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />


<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_below="@+id/textview"
    android:hint="Link"
    android:textColorHint="#ff69ff0e"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Result"
    android:id="@+id/button2"

    android:layout_alignRight="@+id/editText"
    android:layout_alignEnd="@+id/editText" />

Second Activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webView"
    android:layout_gravity="center_horizontal" />
</LinearLayout>

Java Second

public class second extends Activity {
    @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    WebView w1=(WebView)findViewById(R.id.webView);
    w1.loadUrl("http://www.franklinpierce.edu/news.xml");
}

Main Activity Java:

public class MainActivity extends Activity {
EditText title,link,description;
Button b1,b2;
private String finalUrl="rss feed url";
private HandleXML obj;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    link = (EditText) findViewById(R.id.editText);

    b2=(Button)findViewById(R.id.button2);


    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in=new Intent(MainActivity.this,second.class);
            startActivity(in);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {


    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

RSSReader

public class RSSReader {
private String link = "link";
private String urlString = null;
private XmlPullParserFactory xmlFactoryObject;
public volatile boolean parsingComplete = true;

public HandleXML(String url){
    this.urlString = url;
}


public String getLink(){
    return link;
}


public void parseXMLAndStoreIt(XmlPullParser myParser) {
    int event;
    String text=null;

    try {
        event = myParser.getEventType();

        while (event != XmlPullParser.END_DOCUMENT) {
            String name=myParser.getName();

            switch (event){
                case XmlPullParser.START_TAG:
                    break;

                case XmlPullParser.TEXT:
                    text = myParser.getText();
                    break;

                case XmlPullParser.END_TAG:

                    if(name.equals("link")){
                        link = text;
                    }


                    else{
                    }

                    break;
            }

            event = myParser.next();
        }

        parsingComplete = false;
    }

    catch (Exception e) {
        e.printStackTrace();
    }
}

public void fetchXML(){
    Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {

            try {
                URL url = new URL(urlString);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                conn.setReadTimeout(10000 /* milliseconds */);
                conn.setConnectTimeout(15000 /* milliseconds */);
                conn.setRequestMethod("GET");
                conn.setDoInput(true);

                // Starts the query
                conn.connect();
                InputStream stream = conn.getInputStream();

                xmlFactoryObject = XmlPullParserFactory.newInstance();
                XmlPullParser myparser = xmlFactoryObject.newPullParser();

                myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                myparser.setInput(stream, null);

                parseXMLAndStoreIt(myparser);
                stream.close();
            }

            catch (Exception e) {
            }
        }
    });
    thread.start();
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s