MalformedURLException with GSON and JSON

647 views Asked by At

I've got the following error:

Exception in thread "main" java.lang.RuntimeException: java.net.MalformedURLException: no protocol: url at test.getJsonFromUrl(test.java:67) at test.main(test.java:75) Caused by: java.net.MalformedURLException: no protocol: url ...

And this is my complete code:

import java.io.Reader;
import java.net.URL;
import com.google.gson.*;
import java.io.IOException;
import java.io.InputStreamReader;

public class test {

public static String getJsonFromUrl(String url){
Gson gson = new GsonBuilder().create();
try{
    Reader reader = new InputStreamReader(new URL("url").openStream());
    gson.fromJson(reader, String.class);

}catch(IOException e){
        throw new RuntimeException(e);

}

    return gson.toString();
}

public static void main(String[] args) {
    String json = getJsonFromUrl("https://api.kraken.com/0/public/Assets?asset=XBT");
    System.out.println(json);

    }

}

Dont know where the problem is, because if i copy the url in my browser it shows me JSON content... I gues it is an url? How could i fix it?

3

There are 3 answers

0
AndiAbseits On

Okay i changed both and im gettin' the following output:

{serializeNulls:falsefactories:[Factory[typeHierarchy=com.google.gson.JsonElement,adapter=com.google.gson.internal.bind.TypeAdapters$29@52a86356], com.google.gson.internal.bind.ObjectTypeAdapter$1@5ce81285, com.google.gson.internal.Excluder@78c03f1f, Factory[type=java.lang.String,adapter=com.google.gson.internal.bind.TypeAdapters$16@5ec0a365], Factory[type=java.lang.Integer+int,adapter=com.google.gson.internal.bind.TypeAdapters$7@4fe3c938], Factory[type=java.lang.Boolean+boolean,adapter=com.google.gson.internal.bind.TypeAdapters$3@5383967b], Factory[type=java.lang.Byte+byte,adapter=com.google.gson.internal.bind.TypeAdapters$5@2ac273d3], Factory[type=java.lang.Short+short,adapter=com.google.gson.internal.bind.TypeAdapters$6@71423665], Factory[type=java.lang.Long+long,adapter=com.google.gson.internal.bind.TypeAdapters$11@20398b7c], Factory[type=java.lang.Double+double,adapter=com.google.gson.Gson$2@6fc6f14e], Factory[type=java.lang.Float+float,adapte

And so on (its not the complete output). And this is my code:

public static String getJsonFromUrl(String url){
    Gson gson = new GsonBuilder().create();
    try{
        Reader reader = new InputStreamReader(new URL(url).openStream());
        gson.fromJson(reader, JsonObject.class);

    }catch(IOException e){
        throw new RuntimeException(e);

    }

    return gson.toString();
}

public static void main(String[] args) {
    String json = getJsonFromUrl("https://api.kraken.com/0/public/Assets?asset=XBT");
    System.out.println(json);

}
0
epoch On

You are using the string literal, and not the variable...

new URL("url").openStream()

should be

new URL(url).openStream()

note the quotation marks, it indicates that something should be interpreted as a string

3
meda On

The following line is not correct

gson.fromJson(reader, String.class);

First param is stringify json not reader object.

Second param is you model class, not String class.