Skip to content

Instantly share code, notes, and snippets.

@fejd
Created September 20, 2016 12:55
Show Gist options
  • Save fejd/81105496e8bc6c7952b0374583117be3 to your computer and use it in GitHub Desktop.
Save fejd/81105496e8bc6c7952b0374583117be3 to your computer and use it in GitHub Desktop.
Android URL unfurling example
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.unfurl.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<TextView
android:id="@+id/description_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
package com.example.unfurl;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* A simple example of how to use Jsoup to parse HTML metadata to create unfurled previews.
* See the excellent Slack developer post for more info on "unfurling":
* https://medium.com/slack-developer-blog/everything-you-ever-wanted-to-know-about-unfurling-but-were-afraid-to-ask-or-how-to-make-your-e64b4bb9254#.tnspdgalr
*/
public class MainActivity extends AppCompatActivity {
String xperiaUrl = "https://www.komplett" +
".se/product/886519/mobil/mobil/mobiltelefon/sony-xperia-x-performance-32gb-svart";
@BindView(R.id.html_view) TextView htmlView;
@BindView(R.id.title_view) TextView titleView;
@BindView(R.id.description_view) TextView descriptionView;
@BindView(R.id.image_view) ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
UnfurlWebUrlTask unfurl = new UnfurlWebUrlTask();
unfurl.execute(xperiaUrl);
}
class UnfurlWebUrlTask extends AsyncTask<String, Void, UnfurlResult> {
@Override
protected UnfurlResult doInBackground(String... params) {
try {
//<meta property=og:title" content="Content title">
//<meta property=og:description" content="Content description">
//<meta property=og:image" content="Image url">
Document doc = Jsoup.connect(params[0]).get();
Elements metaProperties = doc.select("meta[property]");
UnfurlResult result = new UnfurlResult();
for (Element element : metaProperties) {
// TODO: Quick and dirty, this could probably be improved
String property = element.attr("property");
if (TextUtils.isEmpty(property)) {
continue;
}
switch (property) {
case "og:title":
result.title = element.attr("content");
break;
case "og:description":
result.description= element.attr("content");
break;
case "og:image":
result.imageUrl = element.attr("content");
break;
default:
break;
}
}
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(UnfurlResult result) {
super.onPostExecute(result);
titleView.setText(result.title);
descriptionView.setText(result.description);
Glide.with(getApplicationContext()).load(result.imageUrl).into(imageView);
}
}
class UnfurlResult {
String title;
String description;
String imageUrl;
UnfurlResult() {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment