Skip to content

Commit

Permalink
Add items from DD API feed (#1)
Browse files Browse the repository at this point in the history
- Use okhttp to retrive JSON data
- Use gson to parse JSON into usable objects
- Modify ListActivity to use feed data rather than static list of string
  • Loading branch information
jnovinger committed May 2, 2016
1 parent 0c8d6aa commit b6cc39a
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 16 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.google.code.gson:gson:2.6.2'
}
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.novinger.jason.demoreader" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
73 changes: 57 additions & 16 deletions app/src/main/java/org/novinger/jason/demoreader/ListActivity.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,70 @@
package org.novinger.jason.demoreader;

import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.gson.Gson;

import org.novinger.jason.demoreader.datamodels.Article;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ListActivity extends AppCompatActivity {

private final static int qty = 50;
private final static String feedUrl = "http://www.dailydot.com/api/v1/content/article/";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Article[] articles;
try {
articles = getFeedItems(getFeedUrl());
} catch (IOException ex) {
articles = new Article[0];
Log.d("Exception", ex.toString());
}

final ListView listview = (ListView) findViewById(R.id.listview);
String[] values = new String[] {
"Jason", "Cindy", "Hannah", "Leah", "Kirsten", "Matt", "Elle", "Trevor", "Alex",
"Eli", "Danika", "Lori", "Wyatt", "Chad"
};
final ArrayList<String> list = new ArrayList<String>();
Collections.addAll(list, values);
final ArrayList<String> list = new ArrayList<>();
for (Article article : articles) {
list.add(article.toString());
}

final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
view.animate().setDuration(500).alpha(0)
.withEndAction(new Runnable() {
@Override
public void run() {
list.remove(item);
adapter.notifyDataSetChanged();
view.setAlpha(1);
}
});
final ViewPropertyAnimator viewPropertyAnimator = view.animate().setDuration(100).alpha(0);
viewPropertyAnimator.withEndAction(new Runnable() {
@Override
public void run() {
list.remove(item);
list.trimToSize();
adapter.notifyDataSetChanged();
view.setAlpha(1);
}
});
}
});
}
Expand All @@ -67,4 +90,22 @@ public boolean onOptionsItemSelected(MenuItem item) {

return super.onOptionsItemSelected(item);
}

private Article[] getFeedItems(String url) throws IOException {
OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder().url(url).build();
Response response = client.newCall(request).execute();
String json = response.body().string();
Gson gson = new Gson();
return gson.fromJson(json, Article[].class);
}

private String getFeedUrl() {
if(qty > 0) {
return feedUrl + "?quantity=" + Integer.toString(qty);
}

return feedUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.novinger.jason.demoreader.datamodels;

/**
* Created by jason on 4/27/16.
*/
public class Article {
private String id;
private int realId;
private String headline;
private String slug;
private Author[] authors;
private String summary;
private String url;

public Article() {}

public Article(String id, String headline, String slug, Author[] authors, String url, String summary) {
this.id = id;
this.realId = mungeId(id);
this.headline = headline;
this.slug = slug;
this.authors = authors;
this.url = url;
this.summary = summary;
}

private int mungeId(String id) {
String[] parts = id.split(".");
return Integer.parseInt(parts[2]);
}

public String toString() {
return getHeadline() + " — " + getPrimaryAuthor().getName();
}

public int getId() {
return realId;
}

public String getHeadline() {
return headline;
}

public String getSlug() {
return slug;
}

public Author[] getAuthors() {
return authors;
}

public Author getPrimaryAuthor() {
if (authors.length > 0) {
return authors[0];
}

return new Author();
}

public String getSummary() {
return summary;
}

public String getUrl() {
return url;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.novinger.jason.demoreader.datamodels;

/**
* Created by jason on 4/27/16.
*/
public class ArticleFeed {
private int count;
private String next;
private String previous;
private Article[] results;

public ArticleFeed() {}

public ArticleFeed(int count, String next, String previous, Article[] results) {
this.count = count;
this.next = next;
this.previous = previous;
this.results = results;
}

public Article[] getResults() {
return results;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.novinger.jason.demoreader.datamodels;

/**
* Created by jason on 4/27/16.
*/
public class Author {
private String name;
private String url;
private String slug;

public Author() {}

public Author(String name, String url, String slug) {
this.name = name;
this.url = url;
this.slug = slug;
}

public String toString() {
return name;
}

public String getName() {
return name;
}

public String getUrl() {
return url;
}

public String getSlug() {
return slug;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.novinger.jason.demoreader.datamodels;

/**
* Created by jason on 4/27/16.
*/
public class Headline {
private Boolean headline;
private Boolean title;
private Boolean social;
private String text;

public Headline() {}
}

0 comments on commit b6cc39a

Please sign in to comment.