Skip to content Skip to sidebar Skip to footer

MalformedJsonException: Use JsonReader.setLenient(true) To Accept Malformed JSON - Android Studio

I'm trying to get AdMob ID's into my app from JsonData so that I can change it in the future. My Vision: JSON file will be uploaded into my server and a link of it will be inside t

Solution 1:

You need to read that file from the server first, and then parse the result... It can be as simple as this method:

public String readFileFromServer(String fileUrl) {

    String fileContent;
    try {
        URL url = new URL(fileUrl);
        return new Scanner(url.openStream()).useDelimiter("\\A").next();
    } catch (IOException e) {
        throw new RuntimeException("Error while trying to read file from server!", e);
    }
}

After file content is aquired, just pass it to parse() method.


Solution 2:

You provided url to parse method instead of valid JSON. Each String param by default is treated as JSON. In your case you need to download content and then parse. For example: String interstitial;

String jsonToProcessUrl = "https://drive.google.com/uc?id=1234567890";
try (BufferedInputStream in = new BufferedInputStream(new URL(jsonToProcessUrl).openStream())) {
    interstitial = new JsonParser().parse(new InputStreamReader(in)).getAsJsonObject()
            .get("response").getAsJsonObject()
            .get("Interstial AD").getAsString();
}

System.out.println(interstitial);

See also:


Post a Comment for "MalformedJsonException: Use JsonReader.setLenient(true) To Accept Malformed JSON - Android Studio"