Skip to content

Instantly share code, notes, and snippets.

@lalitsonawane
Created April 23, 2017 16:00
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 lalitsonawane/b43ef17f1716f2a22f9c2a612272ec4f to your computer and use it in GitHub Desktop.
Save lalitsonawane/b43ef17f1716f2a22f9c2a612272ec4f to your computer and use it in GitHub Desktop.
package in.apptonic.lalit.newsarticlesample;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import javax.net.ssl.HttpsURLConnection;
public class MainActivity extends AppCompatActivity {
private TextView resultTextView;
private ArrayList<NewsArticleHolder.ArticleItem> articleItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = (TextView) this.findViewById(R.id.result);
NewsAPIAsyncTask getNewsUpdate = new NewsAPIAsyncTask();
getNewsUpdate.execute();
}
public class NewsAPIAsyncTask extends AsyncTask<Void, Void, ArrayList<NewsArticleHolder.ArticleItem>> {
InputStream inputStream = null;
@Override
protected ArrayList<NewsArticleHolder.ArticleItem> doInBackground(Void... params) {
String result = "";
//connection to url
URL url = null;
try {
url = new URL("https://newsapi.org/v1/articles?source=mashable&sortBy=top&apiKey=eff7c7c5b2b7470e9f785483a9d9520b");
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpsURLConnection urlConnection = null;
try {
urlConnection = (HttpsURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
//pulling data from link
try {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
try {
while ((line = bufferReader.readLine()) != null) {
result += line;
}
} catch (IOException e) {
e.printStackTrace();
}
Log.e("", "result" + result);
JSONObject jsonObject = null;
if (JSONValue.isValidJson(result)) {
jsonObject = (JSONObject) JSONValue.parse(result);
}
JSONArray stories = (JSONArray) jsonObject.get("articles");
String[] JSONKey = MainActivity.this.getResources().getStringArray(R.array.NewsAPIJSONKey);
ArrayList<NewsArticleHolder.ArticleItem> articleItems = new ArrayList<>();
for (int i = 0; i < stories.size(); i++) {
JSONObject entry = (JSONObject) stories.get(i);
NewsArticleHolder.ArticleItem temp = new NewsArticleHolder.ArticleItem(
(String) entry.get(JSONKey[1]),
(String) entry.get(JSONKey[2]),
(String) entry.get(JSONKey[3]),
(String) entry.get(JSONKey[4]),
(String) entry.get(JSONKey[5]),
(String) entry.get(JSONKey[6])
);
articleItems.add(temp);
urlConnection.disconnect();
}
return articleItems;
}
@Override
protected void onPostExecute(ArrayList<NewsArticleHolder.ArticleItem> result) {
MainActivity.this.articleItems = result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment