Getting Java SocketException

huangapple go评论45阅读模式
英文:

Getting Java SocketException

问题

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

java.net.SocketException: 协议族不受支持的地址: 连接
	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 库。

问题是,如果我只调用 getEventData() 而不是从 public static void main() 中调用 Home home = new Home(),那么它是工作的。

提前感谢您的帮助 Getting Java SocketException

英文:

Hello I want to develope a App for my Team, but I'm getting This Error:

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)

My code is this:

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");

  }
}

I'm using the org.json.simple Library.

The thing is if I'm just calling getEventsdata() and not the Home home = new Home() from my public static void main() than it is working.

Thanks in advance for the help Getting Java SocketException

答案1

得分: 2

这是发生在调用不使用IPv4协议栈时的情况。

我猜想Java选项-Djava.net.preferIPv4Stack=true会解决这个问题。

如果你正在使用Eclipse,可以通过JVM程序的命令行参数来设置它。

英文:

This happens if the call doesn't use the IPv4 stack.

I guess that the Java option -Djava.net.preferIPv4Stack=true would do the trick.

If you are using eclipse set it via the jvm program line arguments

huangapple
  • 本文由 发表于 2020年8月2日 04:15:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63209653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定