Next Generation Emulation banner
1 - 15 of 15 Posts

·
Experenced But New User
Joined
·
866 Posts
Discussion Starter · #1 ·
Ok, this is my first year in Java coding. I am taking AP Computer Science at my HighSchool. I have ripped apart a few Java programs I found to make something. The problem is that it my java applet will not get past a certain part in the code, when it tryes to capture the html of a website into a buffer. I did not write this code but I know it works because I it works outside of an applet. This goal of this applet is for people to type the URL of a video on google video and create HTML code to add the video to your own site. You can see this working here on my first ever website. Anyone experenced enuph with Java to help me? Here is the .java file.

Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class Google_GUI extends Applet
{

    private TextField input;
    private TextArea output;
    
    public void init()
    {
        // Construct the TextField, TextArea, and Button
        this.input = new TextField("http://", 50);
        this.output = new TextArea("Insert the URL of the google video you want HTML code to be generated for.", 4, 60);
        this.output.setEditable(false);
        Button button = new Button("Generate");

        // add the stuff to the layout
        this.add(input);
        this.add(button);
        this.add(output);

        // specify that action events sent by the
        // button or the input TextField should be handled 
        // by the same CapitalizerAction object
        GenerateHTML gen = new GenerateHTML();
        button.addActionListener(gen);
        this.input.addActionListener(gen);

        // notice that ActionEvents produced by output are ignored.
    }
    
    private class GenerateHTML implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {
            output.setText("Working");
            String text;
            try
            {
                text = getUserOutput();
            }
            catch (MalformedURLException mue)
            {
                text = "Error: URL";
            }
            catch (IOException mue)
            {
                text = "Error: Buffer";
            }
            output.setText(text);
        }
    }
    
    public String getUserOutput() throws MalformedURLException, IOException
    {
        output.setText("Working -");
        
        URL google = new URL(input.getText());
        
        output.setText("Working - Got");
        //[U][B]The applet gets to here, it displays "Working - Got" in the window but not "Working - Got HTML". I Dont know why the next part of the code doest work inside the applet[/B][/U]
        BufferedReader buffer = new BufferedReader(new InputStreamReader(google.openStream()));
        
        output.setText("Working - Got HTML");
        
        String inputLine;
        String importantString = new String("Error");
        while ( (inputLine = buffer.readLine()) != null)
        {
            if (inputLine.contains("googleplayer.swf"))
            {
                importantString = inputLine;
            }
        }
        buffer.close();
        
        output.setText("Working - Got HTML - Found String");
        
        int length = importantString.length();
        int start = importantString.indexOf("googleplayer.swf");
        int end = importantString.indexOf("\" style=\"width:100%");
      
        String googleVideoCode = new String(importantString.substring(start, end));
       
        int flashWidth = 400;
        int flashHeight = 300;
        
        return "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" width=\"" + flashWidth + "\" height=\"" + flashHeight + "\">\n\t<param name=\"movie\" value=\"" + googleVideoCode + "\" />\n\t<param name=\"quality\" value=\"high\" />\n\t<embed src=\"" + googleVideoCode + "\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"" + flashWidth + "\" height=\"" + flashHeight + "\"></embed>\n</object>";
    }
}
 

·
Transcended
Joined
·
1,416 Posts
I'd love to help but I don't have the time to grab a new SDK. I can't resolve String.contains() for one.

However, here's a tip while the gurus are still out.. try splitting up the code:

InputStream is = google.openStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

Then put setTexts inbetween each. This way, you'll know where exactly it hangs so you can check it out. At first glance, it should work, but it might have some issues with google.openStream(). I haven't personally retrieved streams from a URL object before, so you might wantg to investigate this as the possible culprit first.
 

·
Experenced But New User
Joined
·
866 Posts
Discussion Starter · #3 ·
Yes, my teacher could not help me out either because he did not have Java 5 either.

I will try spliting up the code toamrow in class and see spisifically what is not working. The thing that pisses me off is that it works while its not an applet. I am attaching a java file that I made before turning it into an applet. I attached the code in a text file of the nonapplet verson that works.

Thanks Alot!

PS: We use BlueJ to edit our code.
 

·
Experenced But New User
Joined
·
866 Posts
Discussion Starter · #5 · (Edited)
Really!?!?!?

I have no idea why it wouldent work, its perfect! I just downloaded an upgrade to JAVA today becasue of some popup, so I am going to test it later.

Edit:
Ok, I am really pissed off at java right now (because you know, its java's fault, not mine) and I have uninstalled it from my computer and I am currently installing the newest version I could find. I am really pissed off at the number of different types of java and how they don't have a super uber large one for all download that has everything.

Edit 2:
Ok now I am really getting frustrated, I downloaded some sort of gay server edition. I am going to be really pissed if I have to reinstall it again. Ahhhhhhh!

Edit 3:
This is really pissing me off, I uninstalled the server edition and reinstalled the normal SDK. Everything is back to a perfect clean slate, and I still can't get it to run. Even when I tryed to put the applet on my website it doesnt even start to run. Look here to see what I mean.
 

·
Transcended
Joined
·
1,416 Posts
Hey Kirby, mind telling us what SDK you were using?

Welcome to the wonderful world of Java, Viper_Viper. you should have seen the ruckus caused by Java SDK 1.3. Code which ran perfectly for 1.2 and 1.4 broke on 1.3. :lol:
 

·
Experenced But New User
Joined
·
866 Posts
Discussion Starter · #8 ·
I am currently using "jdk1.5.0_06" and my code wont correctly run in BlueJ. I downloaded Eclipse 3.1 but am still figureing out how it works. I was not able to test my applet correctly, like the "run" box was faded out. Ill look into it more tonight.
 

·
Transcended
Joined
·
1,416 Posts
Kirby said:
Eclipse 3.1
Java SDK 1.5.0-04

:???: :???: :???: :???: :???:
Sorry, my bad for not noticing

@Viper_viper:

Don't bother with Eclipse. It's not the IDE really. Try getting Kirby's exact JDK version.
 

·
Experenced But New User
Joined
·
866 Posts
Discussion Starter · #10 ·
Kraelis said:
Sorry, my bad for not noticing

@Viper_viper:

Don't bother with Eclipse. It's not the IDE really. Try getting Kirby's exact JDK version.
...why would getting a preveous version of java fix things? Maby I should just change some of the code compleatly. I want this to be an applet, widely usuable on the web, so I guess compatablilty is immportant.
 

·
Transcended
Joined
·
1,416 Posts
Like I said... it may be a bug with the SDK. Don't worry... once it's compiled, it shouldn't have any more problems no matter the VM used to run it.

You can try changing it but, so far, the only thing I see questionable in your code is URL to stream. I've only really evry used sockets and not URLs before, and the exact same code works.
 

·
Experenced But New User
Joined
·
866 Posts
Discussion Starter · #12 ·
Kraelis said:
Like I said... it may be a bug with the SDK. Don't worry... once it's compiled, it shouldn't have any more problems no matter the VM used to run it.

You can try changing it but, so far, the only thing I see questionable in your code is URL to stream. I've only really evry used sockets and not URLs before, and the exact same code works.
Sorry, I dident understand you when you said a bug in the SDK, I thought you ment mine might be coruppt. Ok, I am now downloading and installing the Java 5 Update 4 (which I hope means 1.5.0-04) and going to try it out. When putting my applet on the web, I use the .class file right? not the .java file? Thanks alot guys.
 

·
Registered
Joined
·
9,504 Posts
I was also using Linux at the time....shouldn't make any difference, though. Once I get some free time, I'll try it again with JDK 1.5.0-06, Eclipse 3.1.1, and Windows 2000

but like you said, the code looks perfect, even to me (and Eclipse doesn't give any compiling errors)....

edit> I just happened to remember I got a error at that line when I ran the program w/o putting in a valid Goggle Video URL.....I'll look into it more
 

·
Experenced But New User
Joined
·
866 Posts
Discussion Starter · #15 ·
Thanks alot guys. This is my first year in programing and I was getting frustrated. My teacher is a compleate idiot (well, not to be too harsh, it is his first year teaching the class). Me and my friends just play games all day. We try to tell him that the easiest way for us to learn would be to make somthing that we could accualy use, but we keep doing these stupid System.out.print("heylooktext!") labs that dont help us at all.
 
1 - 15 of 15 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top