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

时间:2014-07-12 01:50:11

标签: java

StackOverflow宇宙的明智圣人。我希望你能帮助我解决我遇到的问题。

以下代码的要点是显示符号是否会从yahoo API中获取有效响应。

函数GetSymbol取一个double并返回一个字符串,对于GetSymbol(6149),返回的字符串是#34; IBM"。 (仅供参考,真实的,

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

代码实际上不在函数中,它仅用于我的测试目的。当。。。的时候 symbol =" IBM&#34 ;;不是//外面,它工作正常,但事实并非如此。在实际代码中,调用者线程看起来像if(test(GetSymbol(6149))== 0)。 (0表示无错误,1表示错误)。

出于某种原因,即使GetSymbol(6149)的返回值与" IBM"相等,URL yahootest接受后者但不接受前者。 (使用GetSymbol(6149)作为test()的输入让我不能将URI作为例外。你能想出任何理由吗?

如果您认为它有帮助,我在测试功能下面列出了获取符号功能。它的电话是

    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)

1 个答案:

答案 0 :(得分:1)

如果我不得不猜测,那就是在使用GetSymbol时将4个字符附加在一起创建URL这一事实,但IBM只有三个字符。最有可能在使用GetSymbol而不仅仅是&#34; IBM&#34;时,会有一个隐藏的符号,例如&#34; IBM \ 0&#34;。据我所知,你需要做的是修改你的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; ...),然后将它放在字符串上,因为当你使用网站时它们不喜欢它说&#34; example.com/test \ 0&#34;,他们更喜欢&#34; example.com/test"。不确定我是否完全清楚了,所以只要告诉我你是否有任何问题。简单地说,你正在检查你需要多少个字符并同样设置a,b,c和d,但是无论你知道多长时间,你都会继续追加所有4个字符