Java URL连接

发布于 2024-09-08 10:16:34 字数 851 浏览 7 评论 0原文

简单的事情,我正在课堂上学习 URL/网络,并且我正在尝试在网页上显示一些内容。稍后我将把它连接到 MySQL 数据库...无论如何,这是我的程序:

import java.net.*; import java.io.*;


public class asp {

    public static URLConnection
connection;

    public static void main(String[] args) {

        try {

        System.out.println("Hello World!"); // Display the string.
        try {
        URLConnection connection = new URL("post.php?players").openConnection();
    }catch(MalformedURLException rex) {}
        InputStream response =
connection.getInputStream();
        System.out.println(response);
    }catch(IOException ex) {}

    } }

它编译得很好...但是当我运行它时,我得到:

世界你好!
线程“main”中的异常 java.lang.NullPointerException 在 asp.main(asp.java:17)

第 17 行:InputStream response = connection.getInputStream();

谢谢, 担

Simple stuff, I am learning URLs/Networking in my class and I am trying to display something on a webpage. Later I am going to connect it to a MySQL DB... anyway here is my program:

import java.net.*; import java.io.*;


public class asp {

    public static URLConnection
connection;

    public static void main(String[] args) {

        try {

        System.out.println("Hello World!"); // Display the string.
        try {
        URLConnection connection = new URL("post.php?players").openConnection();
    }catch(MalformedURLException rex) {}
        InputStream response =
connection.getInputStream();
        System.out.println(response);
    }catch(IOException ex) {}

    } }

It compiles fine... but when I run it I get:

Hello World!
Exception in thread "main" java.lang.NullPointerException
at asp.main(asp.java:17)

Line 17: InputStream response = connection.getInputStream();

Thanks,
Dan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云柯 2024-09-15 10:16:34

您有一个格式错误的 URL,但您不会知道,因为您吞下了它的异常

URL("post.php?players")

此 URL 不完整,它缺少主机(对于您来说可能是 localhost?),以及协议部分,比如 http,这样可以避免格式错误的 URL 异常 您必须提供包括协议的完整 URL

new URL("http://www.somewhere-dan.com/post.php?players")

使用 URLConnection。该代码片段至少可以工作,如果您用有效 URL 替换该示例中的 URL,您应该拥有一段有效的代码。

You have a malformed URL, but you wouldn't know because you swallowed its exception!

URL("post.php?players")

This URL is not complete, it misses the host (maybe localhost for you?), and the protocol part, say http so to avoid the malformed URL exception you have to provide the full URL including the protocol

new URL("http://www.somewhere-dan.com/post.php?players")

Use the Sun tutorials on URLConnection first. That snippet is at least known to work, if you substitute the URL in that example with a valid URL you should have a working piece of code.

梦毁影碎の 2024-09-15 10:16:34

这是因为您的网址无效。您需要将完整地址放入您尝试打开连接的页面。您正在捕获 malformedurlexception,但这意味着此时没有“连接”对象。在第一个 catch 块出现之后,您还有一个额外的闭括号。您应该将要获取空指针的行和 system.out.println 放在 catch 块上方

import java.net.*; import java.io.*;

public class asp {

    public static URLConnection connection;

    public static void main(String[] args) {

        try {
        System.out.println("Hello World!"); // Display the string.
            try {
            URLConnection connection = new URL("http://localhost/post.php?players").openConnection();
            InputStream response = connection.getInputStream();
            System.out.println(response);

            }catch(MalformedURLException rex) {
                System.out.println("Oops my url isn't right");
        }catch(IOException ex) {}
    }    
}

It's because your URL is not valid. You need to put the full address to the page you are trying to open a connection to. You are catching the malformedurlexception but that means that there is no "connection" object at that point. You have an extra closed bracket after the first catch block it appears as well. You should put the line that you are getting the null pointer for and the system.out.println above the catch blocks

import java.net.*; import java.io.*;

public class asp {

    public static URLConnection connection;

    public static void main(String[] args) {

        try {
        System.out.println("Hello World!"); // Display the string.
            try {
            URLConnection connection = new URL("http://localhost/post.php?players").openConnection();
            InputStream response = connection.getInputStream();
            System.out.println(response);

            }catch(MalformedURLException rex) {
                System.out.println("Oops my url isn't right");
        }catch(IOException ex) {}
    }    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文