Advertisement
yanivtamari

News

Jul 14th, 2019
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. Json.java
  2. ===============
  3. import org.json.JSONArray;
  4. import org.json.JSONException;
  5. import org.json.JSONObject;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.net.URL;
  12. import java.nio.charset.Charset;
  13.  
  14. public class Json {
  15.     private final String apiURL = "https://newsapi.org/v2/top-headlines?country=il&apiKey";
  16.     private JSONObject json;
  17.     private String apiKEY;
  18.     private String fullURL;
  19.     private String rawData;
  20.     int news;
  21.  
  22.  
  23.     public Json(String apiKEY) throws IOException, JSONException {
  24.         this.apiKEY = apiKEY;
  25.         this.fullURL = apiURL + "=" + apiKEY;
  26.         readJsonFromURL( );
  27.     }
  28.  
  29.     private JSONObject readJsonFromURL() throws IOException, JSONException {
  30.         InputStream is = new URL(this.fullURL).openStream( );
  31.         try {
  32.             BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
  33.             String jsonText = readAll(rd);
  34.             json = new JSONObject(jsonText);
  35.             return json;
  36.         } finally {
  37.             is.close( );
  38.         }
  39.     }
  40.  
  41.     private String readAll(BufferedReader rd) throws IOException {
  42.         StringBuilder sb = new StringBuilder( );
  43.         int cp;
  44.         while ((cp = rd.read( )) != -1) {
  45.             sb.append((char) cp);
  46.         }
  47.         return sb.toString( );
  48.     }
  49.  
  50.     public JSONObject getRawData() throws IOException, JSONException {
  51.         JSONObject json = readJsonFromURL( );
  52.         return json;
  53.     }
  54.  
  55.     public String getarticles() throws IOException, JSONException {
  56.         String title;
  57.         JSONArray articles = json.getJSONArray("articles");
  58.             JSONObject titlenw = articles.getJSONObject(news);
  59.             title =titlenw.getString("title");
  60.         return title;
  61.     }
  62.  
  63.     public String getdescription(int news) throws IOException, JSONException {
  64.         String description;
  65.         JSONArray articles = json.getJSONArray("articles");
  66.             JSONObject desnw = articles.getJSONObject(news);
  67.             description = (String) desnw.get("description");
  68.         return description;
  69.     }
  70. }
  71.  
  72.  
  73. Main.java
  74. ================
  75. import org.json.JSONException;
  76.  
  77. import java.io.IOException;
  78.  
  79. public class Main {
  80.  
  81.     public static void main(String[] args)throws IOException,JSONException {
  82.         String apiKey = "5f3741e0b0d74fa991d4cb3e7ee3dd12";
  83.         System.out.println("Ten First HeadLines: ");
  84.         Json News=new Json(apiKey);
  85.         for (int i=0;i<10;i++){
  86.             System.out.println(News.getarticles());
  87.             //System.out.println(News.getdescription());
  88.         }
  89.  
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement