java - 继续获取 java.net.MalformedURLException : no protocol when setting url with variable

标签 java

当我尝试将 url 从字符串更改为 url 时,我一直没有得到关于 url 的协议(protocol)。任何帮助将不胜感激!

icon_image = weather.weather_pic();
//^ The string icon_image is = http://icons.wxug.com/i/c/k/clear.gif

URL url = new URL("http://icons.wxug.com/i/c/k/clear.gif");


// when I try URL url = new URL(icon_image); it gives me malformed unknown protocal.
// but if i set it like this URL url = new URL("http://icons.wxug.com/i/c/k/clear.gif") it works ??

我添加了 weather_pic 来显示它在做什么

public static String weather_pic() throws IOException {

    // Connect to the URL using java's native library
    String sURL = "http://api.wunderground.com/api/84b167e6ec916b78/conditions/q/NV/Reno.json"; //just a string
    URL url = null;
    try {
        url = new URL(sURL);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    HttpURLConnection request = null;
    try {
        request = (HttpURLConnection) url.openConnection();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        request.connect();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Convert to a JSON object to print data
    JsonParser jp = new JsonParser(); //from gson
    JsonElement root = null; //Convert the input stream to a json element
    try {
        root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
    } catch (IOException e) {
        e.printStackTrace();
    }

    JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object.

    JsonObject cond = rootobj.get("current_observation").getAsJsonObject();

    String icon = cond.get("icon_url").toString();

    System.out.println(icon);
    //String zipcode = rootobj.get("query").getAsString(); //just grab the zipcode
    String icon_x  = icon;



    return icon_x;
}

看看它是否真的有一个值,它确实有

Exception in thread "main" java.net.MalformedURLException: no protocol: "http://icons.wxug.com/i/c/k/partlycloudy.gif"
    at java.net.URL.<init>(URL.java:593)
    at java.net.URL.<init>(URL.java:490)
    at java.net.URL.<init>(URL.java:439)
    at widget.Widget.<init>(Widget.java:42)
    at widget.Widget.main(Widget.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
"http://icons.wxug.com/i/c/k/partlycloudy.gif"

Process finished with exit code 1

最佳答案

您正在使用 cond.get("icon_url").toString(),它用 " 包围字符串结果,就像您的情况一样:

"http://icons.wxug.com/i/c/k/partlycloudy.gif"
^                                            ^

当 URL 发现 " 开始而不是协议(protocol)名称时,它会提示它。

要解决该问题并摆脱那些额外的 " 使用

cond.get("icon_url").getAsString();

关于java - 继续获取 java.net.MalformedURLException : no protocol when setting url with variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40161310/

相关文章:

java - 为什么我们实际上使用 getter 和 setter?

java - 应用程序类可以是包私有(private)的吗?

java - jsp中的变量除了java类可以定义在什么地方?

java - 使用 JUnit5 的集成测试用例不适用于 URL 中包含大括号的 get 请求

java - 如何在此 java 代码中使用数组,以便我可以按照将值分配给位置的顺序输出值?

java - jBPM 6.2 - 如何获取当前分配任务的传出转换

java - Android 测验应用程序,用户通过 Web 表单创建测验内容

Java:扩展对象类

java - 为什么我得到的是 null 而不是 @Inject-ed 字段?

java - Hazelcast : cannot make remote call: com. hazelcast.map.impl.operation.GetOperation