Last active
April 26, 2017 08:13
-
-
Save lalitsonawane/26b04bfe9aa7308d5bffa4f7f81f3180 to your computer and use it in GitHub Desktop.
ViewNotUpdating
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
resultTextView.setText((CharSequence) articleItems); | |
} | |
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[0]), | |
(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]) | |
); | |
articleItems.add(temp); | |
urlConnection.disconnect(); | |
} | |
return articleItems; | |
} | |
@Override | |
protected void onPostExecute(ArrayList<NewsArticleHolder.ArticleItem> result) { | |
MainActivity.this.articleItems = result; | |
resultTextView.setText(""); | |
for (int i =0; i < articleItems.size(); i++){ | |
resultTextView.append(articleItems + ""); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment