Solved

Requesting a URL

Posted on 2004-09-15
9
243 Views
Last Modified: 2010-03-31
I have an application where I need a java program that will open up a URL and cause the web page to be ran.  The java program will be opening a web page that is itself a PHP script.

The code that I have been playing around with is like this:
------------------------------------------------------------------------
import java.net.*;
import java.io.*;

class Link {
      public static void main(String[] args) {
            try
               {
                       URL url = new URL("http://192.168.5.100/index.php");
                        URLConnection connection = url.openConnection();
               }
               catch (Exception e)
               {
                        System.out.println(e.toString());
               }
      }
}
-----------------------------------------------------------------------

Any help would be appreciated.

Also I am using an older version of Java if that matters, I believe it is version 1.2.2.  Please explain your solutions in detail as I have very little Java knowledge.

--
Thanks in Advance
Blaine
0
Comment
Question by:blainehilton
  • 5
  • 3
9 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 12071178
to read the page use:

class Link {
     public static void main(String[] args) {
          try
             {
                    URL url = new URL("http://192.168.5.100/index.php");
                     URLConnection connection = url.openConnection();
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String line = null;
                    while (null!=(line=in.readLine()))
                    {
                       System.out.println(line);
                    }
             }
             catch (Exception e)
             {
                     System.out.println(e.toString());
             }
     }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12071183
if you want to actually render the html page you'll need to use JEditorPane.
Let me know if you need more details.
0
 
LVL 1

Author Comment

by:blainehilton
ID: 12071250
Thank you for your fast response, no I do not need to view the page, just have it run the PHP.

I ran the code you sent and it complied fine but when I ran it I received an error saying "java.net.MalformedURLException".  This is the same error I was having with my example.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 92

Expert Comment

by:objects
ID: 12071278
strange it looks alright, is the URL you are using the same as what is posted here?
0
 
LVL 1

Author Comment

by:blainehilton
ID: 12071305
The URL is pretty much the same, but is different.  It is an IP address and not a hostname and I am using the http:// and the page does exist, and the device can ping the server.

Does it matter that this is running on a TINI Module?  (http://www.ibutton.com/TINI/index.html)

When I run it with the command "java Link.class" from the DOS prompt I receive a different error.  The dos prompt gives me:

Exception in thread "main" java.lang.NoClassDefFoundError: Link/class

0
 
LVL 92

Expert Comment

by:objects
ID: 12071393
> When I run it with the command "java Link.class"

should be:

java Link
0
 
LVL 92

Expert Comment

by:objects
ID: 12071394
> The URL is pretty much the same, but is different.

what is it exactly then?
0
 
LVL 1

Author Comment

by:blainehilton
ID: 12071467
Using the command "java Link" it worked great, however it is not working on the TINI.
0
 
LVL 2

Accepted Solution

by:
SergeiKo earned 250 total points
ID: 12074346
Hello, blainehilton.

I have some practice in working with TINI. So I have some advices for you.

1. Tune your build command, you have to add HTTP classes, they are not included in the Core API
    (see <TINISDK>/docs/BuildDependency.txt for details).
    Here is the build file for my test class, which source I provide at the end of this question.

    Compiling classes:
javac.exe -classpath C:\Projects\TINISDK\tini1.13\bin\tiniclasses.jar -d bin -target 1.1 *.java

    Building .tini module.
java -classpath C:\Projects\TINISDK\tini1.13\bin\tini.jar BuildDependency -x C:\Projects\TINISDK\tini1.13\bin\owapi_dep.txt -p C:\Projects\TINISDK\tini1.13\bin\owapi_dependencies_TINI.jar;C:\Projects\TINISDK\tini1.13\bin\modules.jar;bin -f bin -o MiniBrowser.tini -d C:\Projects\TINISDK\tini1.13\bin\tini.db -add HTTP

    -f parameter is about the path to your compiled classes.
    About  -add  parameter see <TINISDK>/docs/BuildDependency.txt.

    After that copy *.tini file to the TINI and run, e.g.
java MiniBrowser.tini


//Example from The TINI Specification and Developer's Guide  Don Loomis
import java.net.*;
import java.io.*;
import com.dalsemi.tininet.TININet;

class MiniBrowser {
      public static void main( String[] args ) {
            try {
                  URL u = new URL(args[0]);
                  InputStream in = u.openConnection().getInputStream();

                  byte[] content = new byte[512];
                  int count = 0;
                  do {
                        count = in.read(content);
                        System.out.write(content, 0, count);
                  } while (count != -1);
            } catch ( Exception e ) {
                  System.out.println( e.getMessage() );
                  e.printStackTrace();
            }
      }
}


2. Visit http://discuss.dalsemi.com/ for more info on TINI.


Regards.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
varialbe initialization 11 33
javap not working 8 44
runtime exception 2 27
How to determine if a string is a valid SHA value 7 30
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question