Skip to content

Instantly share code, notes, and snippets.

@lacucaracha-jp
Created January 30, 2016 01:23
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 lacucaracha-jp/b97ccafc23488c4de5d2 to your computer and use it in GitHub Desktop.
Save lacucaracha-jp/b97ccafc23488c4de5d2 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import net.arnx.jsonic.JSON;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Amano
*/
public class HatenaBookmark {
static InputStream in = null;
static HttpURLConnection connect = null;
static String url = null;
static InputStream is = null;
static String str = null;
public static void main(String[] args) {
try {
url = "http://b.hatena.ne.jp/entry/jsonlite/?url=" + URLEncoder.encode(args[0], "utf-8");;
connect = (HttpURLConnection) new URL(url).openConnection();
connect.setRequestMethod("GET");
connect.setConnectTimeout(100000);
is = connect.getInputStream();
str = convertString(is);
Page page = JSON.decode(str, Page.class);
for(Bookmark bookmark:page.getBookmarks()){
System.out.println(bookmark.getUser()+","+bookmark.getComment());
}
System.out.println(page.getTitle());
} catch (Exception e) {
System.out.println(e);
}
}
static String convertString(InputStream is) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
char[] b = new char[1024];
int line;
while (0 <= (line = reader.read(b))) {
sb.append(b, 0, line);
}
return sb.toString();
}
}
class Bookmark {
private Date timestamp;
private String comment;
private String user;
private String tag;
public Date getTimestamp() {
return timestamp;
}
public String getComment() {
return comment;
}
public String getUser() {
return user;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public void setComment(String comment) {
this.comment = comment;
}
public void setUser(String user) {
this.user = user;
}
public String getTags() {
return tag;
}
public void setTags(String tag) {
this.tag = tag;
}
}
class Page {
public int count;
public String url;
public String eid;
public String title;
public String screenshot;
public String entry_url;
private List<Bookmark> bookmarkList;
public void setBookmarks(List<Bookmark> bookmarkList) {
this.bookmarkList = bookmarkList;
}
public List<Bookmark> getBookmarks() {
return bookmarkList;
}
public int getCount() {
return count;
}
public String getUrl() {
return url;
}
public String getEid() {
return eid;
}
public String getTitle() {
return title;
}
public String getScreenshot() {
return screenshot;
}
public String getEntry_url() {
return entry_url;
}
public void setCount(int count) {
this.count = count;
}
public void setUrl(String url) {
this.url = url;
}
public void setEid(String eid) {
this.eid = eid;
}
public void setTitle(String title) {
this.title = title;
}
public void setScreenshot(String screenshot) {
this.screenshot = screenshot;
}
public void setEntry_url(String entry_url) {
this.entry_url = entry_url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment