2

I keep getting no protocol on a url when I try to change it from a string to a url. Any help would be greatly appreciated!

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 ??

I added weather_pic to show what its doing

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;
}

See if its actually has a value and it does

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
13
  • Are you sure weather.weather_pic() is returning some value? Commented Oct 20, 2016 at 17:56
  • try to print icon_image once . Commented Oct 20, 2016 at 17:57
  • Have you considered that you may be wrong about assuming that icon_image holds that string? From my experience, Java doesn't lie, so if it says that it can't find some data, then either that data is not there, or it is there, but in incorrect format (maybe it contains some additional characters). Commented Oct 20, 2016 at 17:58
  • 1
    I added weather_pic to question @Pshemo Commented Oct 20, 2016 at 18:01
  • 1
    by the way you have many try-catch clauses that catch IOException, I suggest you merge them into one(but leave the first one that catches MalformedURLException) Commented Oct 20, 2016 at 18:10

1 Answer 1

9

You are using cond.get("icon_url").toString() which surrounds string result with " like in your case:

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

When URL finds that " at start instead of protocol name it complains about it.

To solve that problem and get rid of those extra " use

cond.get("icon_url").getAsString();
Sign up to request clarification or add additional context in comments.

1 Comment

You are awesome that did it @Pshemo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.