// News extension for App Inventor by Marco Perrone package News; import com.google.appinventor.components.runtime.*; import android.app.Activity; import android.content.Intent; import com.google.appinventor.components.annotations.DesignerComponent; import com.google.appinventor.components.annotations.DesignerProperty; import com.google.appinventor.components.annotations.PropertyCategory; import com.google.appinventor.components.annotations.SimpleFunction; import com.google.appinventor.components.annotations.SimpleEvent; import com.google.appinventor.components.annotations.SimpleObject; import com.google.appinventor.components.annotations.SimpleProperty; import com.google.appinventor.components.annotations.UsesPermissions; import com.google.appinventor.components.annotations.UsesActivities; import com.google.appinventor.components.annotations.UsesLibraries; import com.google.appinventor.components.annotations.UsesActivityMetadata; import com.google.appinventor.components.annotations.androidmanifest.*; import com.google.appinventor.components.common.ComponentCategory; import com.google.appinventor.components.common.PropertyTypeConstants; import com.google.appinventor.components.common.YaVersion; import com.google.appinventor.components.runtime.util.GingerbreadUtil; import com.google.appinventor.components.runtime.util.SdkLevel; import com.google.appinventor.components.runtime.util.AsynchUtil; import com.google.appinventor.components.runtime.util.YailList; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.net.URI; import java.net.URISyntaxException; import java.net.CookieHandler; import java.util.Scanner; import java.util.Iterator; import java.util.Map; import java.util.List; import java.util.ArrayList; import java.io.FileWriter; import java.io.BufferedWriter; import java.io.StringWriter; import java.io.PrintWriter; import java.io.IOException; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.DataOutputStream; import java.io.BufferedReader; import java.io.OutputStream; @SimpleObject(external=true) @UsesPermissions(permissionNames = "android.permission.INTERNET") @DesignerComponent(version = YaVersion.WEB_COMPONENT_VERSION, description = "
Non-visible component to search news from all over the world.",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "images/nearfield.png")
public class News extends AndroidNonvisibleComponent implements Component {
private final Activity activity;
private final CookieHandler cookieHandler;
private String twoHyphens = "--";
private String boundary = "*****"+Long.toString(System.currentTimeMillis())+"*****";
private String LINE = "\r\n";
private URL url;
private HttpURLConnection httpConn = null;
private OutputStream outputStream;
private PrintWriter writer;
private String charset = "utf-8";
@SimpleEvent(description = "Got News")
public void GotNews(int responseCode, String response, YailList source, YailList author, YailList title, YailList description, YailList urlArticle, YailList urlImage, String message) {
EventDispatcher.dispatchEvent(this, "GotNews", responseCode, response, source, author, title, description, urlArticle, urlImage, message);
}
@SimpleFunction(description = "Get News")
public void GetNews(final String query, final String from, final String to, final String language, final String sortby, final String pagesize, final String apikey) {
AsynchUtil.runAsynchronously(new Runnable() {
@Override
public void run() {
String URLStr = "https://newsapi.org/v2/everything?";
if (pagesize.length() != 0)
{
URLStr = URLStr + "pagesize=" + pagesize;
}
else
{
URLStr = URLStr + "pagesize=10";
}
if (query.length() != 0)
{
URLStr = URLStr + "&q=" + query;
}
if (from.length() != 0)
{
URLStr = URLStr + "&from=" + from;
}
if (to.length() != 0)
{
URLStr = URLStr + "&to=" + to;
}
if (language.length() != 0)
{
URLStr = URLStr + "&language=" + language;
}
else
{
URLStr = URLStr + "&language=en";
}
if (sortby.length() != 0)
{
URLStr = URLStr + "&sortby=" + sortby;
}
else
{
URLStr = URLStr + "&sortby=relevance";
}
GetNews_Exec(URLStr, apikey);
}
});
}
@SimpleFunction(description = "Get Top Headlines")
public void GetTopHeadlines(final String query, final String category, final String country, final String pagesize, final String apikey) {
AsynchUtil.runAsynchronously(new Runnable() {
@Override
public void run() {
String URLStr = "https://newsapi.org/v2/top-headlines?";
if (pagesize.length() != 0)
{
URLStr = URLStr + "pagesize=" + pagesize;
}
else
{
URLStr = URLStr + "pagesize=10";
}
if (query.length() != 0)
{
URLStr = URLStr + "&q=" + query;
}
if (category.length() != 0)
{
URLStr = URLStr + "&category=" + category;
}
if (country.length() != 0)
{
URLStr = URLStr + "&country=" + country;
}
GetNews_Exec(URLStr, apikey);
}
});
}
public News(ComponentContainer container) {
super(container.$form());
activity = container.$context();
cookieHandler = (SdkLevel.getLevel() >= SdkLevel.LEVEL_GINGERBREAD)
? GingerbreadUtil.newCookieManager()
: null;
}
void GetNews_Exec(String URLStr, String APIKEY) {
try
{
InputStream inputStream = null;
URL url = new URL(URLStr);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
httpConn.setRequestProperty("Authorization", APIKEY);
final int status = httpConn.getResponseCode();
List