Permalink
Browse files

Convert JSON string to POJO (plain old Java objects)

  • Loading branch information...
dragosholban committed Mar 14, 2018
1 parent 0e79523 commit 2ab2d0eff007a31242aba80baa2c4ba24cbd56f3
@@ -26,4 +26,5 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile 'com.google.code.gson:gson:2.8.2'
}
@@ -1,37 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dragosholban.com.bestphotos">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<activity android:name=".MainActivity"
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
<activity android:name=".ImagesActivity"></activity>
</application>
</manifest>
@@ -0,0 +1,9 @@
package dragosholban.com.bestphotos;
public class FacebookImage {
public String fbId;
public String url;
public String link;
public long createdAt;
public int reactions;
}
@@ -0,0 +1,80 @@
package dragosholban.com.bestphotos;
import java.util.List;
public class FacebookPhotos {
public class Datum {
public class Reactions {
public class Datum_ {
public String id;
public String name;
public String type;
}
public class Paging {
public class Cursors {
public String before;
public String after;
}
public Cursors cursors;
public String next;
}
public class Summary {
public Integer total_count;
public String viewer_reaction;
}
public List<Datum_> data = null;
public Paging paging;
public Summary summary;
}
public class Image {
public Integer height;
public String source;
public Integer width;
}
public String picture;
public Reactions reactions;
public String link;
public List<Image> images = null;
public String created_time;
public String id;
}
public class Paging_ {
public class Cursors_ {
public String before;
public String after;
}
public Cursors_ cursors;
public String next;
}
public List<Datum> data = null;
public Paging_ paging;
}
@@ -0,0 +1,91 @@
package dragosholban.com.bestphotos;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.facebook.AccessToken;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.google.gson.Gson;
import org.json.JSONObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class ImagesActivity extends AppCompatActivity {
private static final String TAG = ImagesActivity.class.getName();
private ArrayList<FacebookImage> images = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_images);
GraphRequest.Callback callback = new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
JSONObject json = response.getJSONObject();
Log.d(TAG, "Photos: " + json.toString());
Gson gson = new Gson();
FacebookPhotos fbPhotos = gson.fromJson(json.toString(), FacebookPhotos.class);
for (FacebookPhotos.Datum datum : fbPhotos.data) {
Log.d(TAG, "Image URL: " + datum.picture);
FacebookImage image = new FacebookImage();
image.fbId = datum.id;
image.reactions = datum.reactions.summary.total_count;
image.link = datum.link;
image.url = datum.picture;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss+SSSS");
Date date = new Date();
try {
date = format.parse(datum.created_time);
} catch (ParseException e) {
e.printStackTrace();
}
image.createdAt = date.getTime();
// find the closest image to 300x300px
FacebookPhotos.Datum.Image imageSource = null;
for (FacebookPhotos.Datum.Image im : datum.images) {
if (imageSource == null) {
imageSource = im;
} else {
if (imageSource.width < imageSource.height) {
if (imageSource.width <= 300 && imageSource.width < im.width || imageSource.width > 300 && im.width > 300 && imageSource.width > im.width) {
imageSource = im;
}
} else {
if (imageSource.height <= 300 && imageSource.height < im.height || imageSource.height > 300 && im.height > 300 && imageSource.height > im.height) {
imageSource = im;
}
}
}
}
if (imageSource != null) {
image.url = imageSource.source;
}
images.add(image);
}
if (fbPhotos.paging != null && fbPhotos.paging.cursors.after != null) {
GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "me/photos?fields=picture,reactions.limit(1).summary(true),link,images,created_time&type=uploaded&limit=500&after=" + fbPhotos.paging.cursors.after, null, HttpMethod.GET, this);
request.executeAsync();
}
}
};
GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "me/photos?fields=picture,reactions.limit(1).summary(true),link,images,created_time&type=uploaded&limit=500", null, HttpMethod.GET, callback);
request.executeAsync();
}
}
@@ -1,22 +1,19 @@
package dragosholban.com.bestphotos;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import org.json.JSONObject;
import java.util.Arrays;
import java.util.Set;
@@ -32,6 +29,8 @@ protected void onCreate(Bundle savedInstanceState) {
LoginButton loginButton = findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("user_photos"));
final Activity activity = this;
// Callback registration
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@@ -49,17 +48,10 @@ public void onSuccess(LoginResult loginResult) {
}
if (granted) {
GraphRequest.Callback callback = new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
JSONObject json = response.getJSONObject();
Log.d(TAG, "Photos: " + json.toString());
}
};
GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "me/photos?fields=picture,reactions.limit(1).summary(true),link,images,created_time&type=uploaded&limit=500", null, HttpMethod.GET, callback);
request.executeAsync();
Intent intent = new Intent(activity, ImagesActivity.class);
activity.startActivity(intent);
} else {
Toast.makeText(activity, "Please allow us to access your Facebook photos in order to show you the best ones.", Toast.LENGTH_LONG).show();
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="dragosholban.com.bestphotos.ImagesActivity">
</android.support.constraint.ConstraintLayout>

0 comments on commit 2ab2d0e

Please sign in to comment.