0

I'm trying to get articles from a Newsapi.org and parse them in Java into a String to send these articles out with my Java Telegram-Abilities Bot. I tried using different method's for parsing the JSON in java.

import org.json.*;
import org.apache.http.*;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public String GetNews() {
  StringBuilder random = new StringBuilder();
  StringBuilder failed = new StringBuilder();
  String json = ("https://newsapi.org/v2/top-headlines?country=de&category=business&apiKey=myapikeyhere");
   try {
    JsonObject jsonObject = new
    JsonParser().parse(json).getAsJsonObject(); // PROBLEM HERE!!!! Throws 
    com.google.gson.stream.MalformedJsonException
    String article =
     jsonObject.getAsJsonObject("id").get("name").getAsString();
    System.out.println(article);


    //String status = 
    obj.getJSONObject("source").getString("name");

    JsonArray arr = jsonObject.getAsJsonArray("articles");
    for (int i = 0; i < 3; i++) {
     String post_id =
      arr.get(i).getAsJsonObject().get("id").getAsString();
     System.out.println(post_id);
     random.append(post_id);
    }
    return random.toString();
   } catch (JsonSyntaxException ex) {
    String post_id = ("Critical Error");
    failed.append(post_id);
   }
   return failed.toString();
}

com.google.gson.stream.MalformedJsonException

Blobslam
  • 13
  • 4
  • Welcome to SO. Please read [ask] and note that we'll need more/other information in order to help. In general when asking about exceptions you should post the stacktrace and in particular if `JsonParser.parse(json)` throws a `MalformedJsonException` then we'd have to see the json that the parser complains about. – Thomas Jun 13 '19 at 12:46
  • Please post the json that causes the exception – atarasenko Jun 13 '19 at 12:51
  • it's to long to post the content here, you can find the json's here https://newsapi.org/ – Blobslam Jun 13 '19 at 12:59

1 Answers1

0

Are you missing out the API GET method which is actually pulling your response?

String json = ("newsapi.org/v2/top-headlines? country = de & category = business & apiKey = myapikeyhere ");

is currently just a string, as what is returned from that API is a well-formed JSON object

juju
  • 553
  • 1
  • 4
  • 21
  • isn't this ```JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();``` the API GET method? – Blobslam Jun 13 '19 at 13:26
  • No. That is just setting up the JsonParser to parse whatever is defined in String json into jsonObject. You'll need a way to actually pull the data from the API, instead of just writing the URL in a String. Have a look at https://www.baeldung.com/java-http-request, or https://alvinalexander.com/java/java-apache-httpclient-restful-client-examples for REST examples. May I suggest that you do a little reading about API clients also. And maybe check : https://stackoverflow.com/questions/3913502/restful-call-in-java#answer-3916369 – juju Jun 13 '19 at 13:30