This is my code. I don't know exactly why the program doesn't work. If I press the button, it should display the corresponding quote and its author. But nothing is displayed. After experimenting, I found out that the try clause of the exception DIDN'T REALLY work. I need help on this one. About the loop, I am just going to display 1 quote and author. :) Thank you DaniWeb

package com.example.xmlfinalproject;

import android.os.*;
import android.app.*;
import android.view.*;
import android.widget.*;

import java.net.URL;
//import java.util.Random;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class MainActivity extends Activity {

    Button b1;
    TextView tvAuthor, tvQuotes, tvProjectTitle;
    EditText txtAuthor, txtQuotes;



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

    //  Random get = new Random();

    //  int seed = get.nextInt(10-1 + 1) + 1;

        b1 = (Button)findViewById(R.id.button1);
        tvProjectTitle = (TextView)findViewById(R.id.lblProjectTitle);
        tvAuthor = (TextView)findViewById(R.id.lblAuthor);
        tvQuotes = (TextView)findViewById(R.id.lblQuote);
        txtAuthor = (EditText)findViewById(R.id.txtAuthor);
        txtQuotes = (EditText)findViewById(R.id.txtQuote);

        b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {
            //  Random get = new Random();

            //  int seed = get.nextInt(10-1 + 1) + 1;

                URL url = new URL("http://ghettodoggs.bugs3.com/quotes.xml");
                DocumentBuilderFactory x = DocumentBuilderFactory.newInstance();
                DocumentBuilder y = x.newDocumentBuilder();
                Document z = y.parse(new InputSource(url.openStream()));


                NodeList quote = z.getElementsByTagName("quote");
                NodeList author = z.getElementsByTagName("author");

                for(int i = 0; i < 1; i++)
                {
                Element gatherQuotes = (Element) quote.item(3);
                Element gatherAuthors = (Element) author.item(3);
                String quotes = gatherQuotes.getFirstChild().getNodeValue();
                String authors = gatherAuthors.getFirstChild().getNodeValue();

                txtAuthor.setText(authors);
                txtQuotes.setText(quotes);

                }
            }



                catch (Exception e)
                {                   
                    txtAuthor.setText("hello");
                }
        }
        });

    }

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

}

Recommended Answers

All 7 Replies

Your catch block is passed an Exception to tell you exactly what went wrong. You should always start by printing all that info:

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

The program runs fine. But no output is displayed :(

What's your definition of "runs fine"? In my book a program that does not produce the desired output is anything but "fine". Have you added the code to print any Exceptions?

The program reaches runtime without any problems. The problem is that it displays nothing. Yes I have tried putting printStackTrace() but it displayed nothing. Is there lacking with my code?

I can't say anything about the code because your class extends other classes that I don't have the source code for. If you think the problem is somewhere in that try block, just insert a whole load of System.out.println statements to print the key variables at various points in the block, so you can see what's being executed and what values are being used.

I did some experimenting because I am a little rusty at Android java, but I managed to get something like what you want working. I think your big mistake is accessing the Internet from the UI thread. I'm surprised that doesn't cause an exception for you because my Android emulator threw an exception because of that when I copied your code.

You need a java.util.concurrent.Executor and an android.os.Handler. The Executor executes your Internet access and the Handler executes the modifications to the UI that you do after each Internet access. So in your onCreate you want something like this:

executor = java.util.concurrent.Executors.newSingleThreadExecutor();
handler = new Handler(); // Creating the handler on this thread means it will run on this thread.
b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // We can't access the Internet on this thread, so ask the executor to do it
        executor.execute(new Runnable() {
            @Override
            public void run() {
                showQuote();
            }
        });
    }
});

Using an Executor allows us to start a job on another thread without creating a new thread every time. I used a executor field and a handler field. I created a showQuote private method because putting the Internet access directly in the run method would be nesting too deeply to be readable. Inside the showQuote method, after I read the quote, I did something like this:

handler.post(new Runnable() {
    @Override
    public void run() {
        EditText txtAuthor = (EditText)findViewById(R.id.txtAuthor);
        EditText txtQuotes = (EditText)findViewById(R.id.txtQuote);
        txtAuthors.setText(authors);
        txtQuotes.setText(quotes);
    }
});

Handler is the tool that allows you to ask a thread to do something even though you are in a different thread. If you tried to call setText from the executor thread, you would just get an exception; Android is much more careful about thread-safety than Swing.

Once I took care of that issue, the app worked without problems.

Sorry for the very late reply, I have solved the problem way back months ago! I just forgot to put something on AndroidManifest.xml!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.