Skip to content

Instantly share code, notes, and snippets.

@coding-catie
Created September 24, 2018 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coding-catie/9773852505b0122dccb05ad1186a280a to your computer and use it in GitHub Desktop.
Save coding-catie/9773852505b0122dccb05ad1186a280a to your computer and use it in GitHub Desktop.
Mayat - News App MyNewsQueryUtils
package com.example.android.mynewsappone;
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class MyNewsQueryUtils {
public static List<MyNews> fetchNewsData(String requestUrl) {
URL url = createUrl(requestUrl);
String jsonResponse = null;
try {
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
e.printStackTrace();
}
List<MyNews> newsList = extractFeatureFromJson(jsonResponse);
return newsList;
}
private static URL createUrl(String stringUrl) {
URL url = null;
try {
url = new URL(stringUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return url;
}
private static String makeHttpRequest(URL url) throws IOException {
String jsonResponse = "";
if (url == null) {
return jsonResponse;
}
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
// urlConnection.setResponseCode(200/*milliseconds */);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
if (urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} else {
Log.d("Error response code: ", String.valueOf(urlConnection.getResponseCode()));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
}
return jsonResponse;
}
private static String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
private static List<MyNews> extractFeatureFromJson(String newsJSON) {
if (TextUtils.isEmpty(newsJSON)) {
return null;
}
List<MyNews> newsList = new ArrayList<>();
try {
JSONObject baseJsonResponse = new JSONObject(newsJSON);
JSONObject response = baseJsonResponse.getJSONObject("response");
JSONArray resultsArray = response.getJSONArray("results");
for (int i = 0; i < resultsArray.length(); i++) {
JSONObject currentResults = resultsArray.getJSONObject(i);
String Title = currentResults.getString("webTitle");
String category = currentResults.getString("sectionName");
String date = currentResults.getString("webPublicationDate");
String url = currentResults.getString("webUrl");
JSONArray tagsauthor = currentResults.getJSONArray("tags");
String author="";
if (tagsauthor.length()!= 0) {
JSONObject currenttagsauthor = tagsauthor.getJSONObject(0);
author = currenttagsauthor.getString("webTitle");
}else{
author = "No Author ..";
}
MyNews news = new MyNews(Title, category, date, url, author);
newsList.add(news);
}
} catch (final JSONException e) {
e.printStackTrace();
}
return newsList;
}
}
// catch (final Exception e) {
// throw new RuntimeException("Error initializing TensorFlow!", e);
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment