JAVA URI 不能为空 - 协助此功能

  • 本文关键字:功能 URI 不能 JAVA java
  • 更新时间 :
  • 英文 :


嗨,StackOverflow宇宙的智者们。我希望你能帮我解决一个问题。

下面代码的目的是显示该符号是否会从yahoo API请求有效的响应。

函数GetSymbol接受一个double类型并返回一个字符串,在GetSymbol(6149)的情况下,返回的字符串是"IBM"。(仅供参考,在现实中,

    symbol = GetSymbol(6149);
    //symbol = "IBM";           // This is to show that it works normally

代码并不真正在函数中,它只是为了我的测试目的而存在。当symbol = "IBM";不是//out,它工作得很好,但这样它就不工作了。在实际代码中,调用者线程看起来像if(test(GetSymbol(6149)) == 0).(0表示没有错误,1表示错误)。

由于某种原因,即使GetSymbol(6149)的返回值等于"IBM",URL yahootest接受后者而不接受前者。(使用GetSymbol(6149)作为test()的输入给我URI不能为空作为异常。你能想到原因吗?

如果你认为它有帮助,我在测试函数下面列出了Get Symbol函数。其调用是

    while(i <= Math.pow(26,4)){
    GetSymbol(i);
    i++;
    }

这是测试函数

public int test(String symbol){
    int testnum = 0;
    symbol = GetSymbol(6149);
    //symbol = "IBM";           // This is to show that it works normally
    String url = "http://download.finance.yahoo.com/d/quotes.csv?s="
            + symbol + "&f=nsl1op&e=.csv";

        try{
        URL yahootest = new URL(url);
        URLConnection data = yahootest.openConnection();
        Scanner input = new Scanner(data.getInputStream());

    }catch(Exception e){
        System.out.println(e.getMessage());
        testnum++;
    }
    return testnum;
}

这是GetSymbol函数

 public  String GetSymbol(double i){        
    double parts = Math.floor(i/26);
    double remainder = Math.floor(parts/26);
    double remainder2 = Math.floor(remainder/26);

    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    String e = "%5E";

    if(i <= 26){
        a = (int) (i + 64);
    }
    else if(i <= Math.pow(26,2)){
        a = (int) parts;
        b = (int) (i - (26*a));
        a = a + 64;
        b = b + 64;
        if(b == 64){b++;}
    }
    else if((i <= Math.pow(26,3))){
        a = (int) remainder;
        b = (int) (parts -(remainder*26));
        c = (int) (i - (parts*26));
        a = a + 64;
        b = b + 64;
        c = c + 64;
        if(b == 64){b++;}
        if(c == 64){c++;}
    }
    else if((i <= Math.pow(26,4))){
        a = (int) remainder2;
        b = (int) (remainder - (remainder2*26));
        c = (int) (parts - (remainder * 26));
        d = (int) (i - (parts*26));
        a = a + 64;
        b = b + 64;
        c = c + 64;
        d = d + 64;
        if(b == 64){b++;}
        if(c == 64){c++;}
        if(d == 64){d++;}       
    }
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(String.valueOf((char) a));
    stringBuilder.append(String.valueOf((char) b));
    stringBuilder.append(String.valueOf((char) c));
    stringBuilder.append(String.valueOf((char) d));
    String string = stringBuilder.toString();

    symbol = string;
     return symbol;
 }

这是整个堆栈跟踪

java.lang.IllegalArgumentException: URI can't be null.
at sun.net.spi.DefaultProxySelector.select(DefaultProxySelector.java:147)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1097)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:997)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:931)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at Stocks.Begin.test(BasicApp.java:185)
at Stocks.Begin.<init>(BasicApp.java:42)
at Stocks.BasicApp.main(BasicApp.java:21)

如果要我猜的话,应该是在使用GetSymbol时将4个字符加在一起创建URL,而IBM只有3个字符。最有可能的是,当使用GetSymbol而不仅仅是"IBM"时,有一个隐藏的符号,比如"IBM"。据我所知,你需要做的是修改你的GetSymbol函数,如下所示:

StringBuilder stringBuilder = new StringBuilder();
if(a > 0) stringBuilder.append(String.valueOf((char) a));
if(b > 0) stringBuilder.append(String.valueOf((char) b));
if(c > 0) stringBuilder.append(String.valueOf((char) c));
if(d > 0) stringBuilder.append(String.valueOf((char) d));
String string = stringBuilder.toString();

这样做的目的是确保每个字符不是你之前设置的默认0 (int a = 0;),因为网站不喜欢你说"example.com/test",他们更喜欢"example.com/test"。我不确定我是否完全讲清楚了,所以如果你有任何问题请告诉我。简单地说,您检查了需要多少个字符,并设置了a、b、c和d,但是无论您知道

有多长,您都继续追加所有4个字符。

最新更新