获取 Java SocketException

Getting Java SocketException

你好,我想为我的团队开发一个应用程序,但我收到了这个错误:

java.net.SocketException: Address family not supported by protocol family: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:606)
    at java.net.Socket.connect(Socket.java:555)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
    at sun.net.www.http.HttpClient.New(HttpClient.java:339)
    at sun.net.www.http.HttpClient.New(HttpClient.java:357)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1226)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1162)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1056)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:990)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1570)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
    at java.net.URL.openStream(URL.java:1067)
    at de.tbprivi.mgde.Home.Home.getEventData(Home.java:37)
    at de.tbprivi.mgde.Home.Home.<init>(Home.java:29)
    at de.tbprivi.mgde.main.Main.main(Main.java:8)

我的代码是这样的:

package de.tbprivi.mgde.Home;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class Home {

 public JFrame frame;
 private JSONParser parser;

 public Home(){

    frame = new JFrame();
    frame.setTitle("Miners-Games.de - TEAM APP");
    frame.setSize(700,500);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);

    try {
        getEventData();
    } catch (IOException | ParseException e) {
        e.printStackTrace();
    }

 }
 private void getEventData() throws IOException, ParseException {

    Object obj = parser.parse(new InputStreamReader(new URL("http://mylink.com/events.json").openStream()));

    JSONObject file = (JSONObject) obj;

    file.get("events");

  }
}

我正在使用 org.json.simple 库。

如果我只是调用 getEventsdata() 而不是 Home home = new Home() public static void main() 比它正在工作。

在此先感谢您的帮助:)

如果调用不使用 IPv4 堆栈,就会发生这种情况。

我想 Java 选项 -Djava.net.preferIPv4Stack=true 可以解决问题。

如果您使用的是 eclipse,请通过 jvm 程序行参数设置它