提交 57f387c4 编写于 作者: K Kiran Ryali

Merge pull request #86 from square/jw/twitter-example

Add simple Twitter example using synchronous API.
......@@ -20,5 +20,6 @@
<modules>
<module>directory-sync</module>
<module>twitter-client</module>
</modules>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2012 Square, Inc.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.squareup</groupId>
<artifactId>retrofit-examples</artifactId>
<version>0.8.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>retrofit-example-twitter-client</artifactId>
<packaging>jar</packaging>
<name>Example: Twitter Client</name>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>retrofit-http</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
// Copyright 2012 Square, Inc.
package com.squareup.retrofit.example.twitter;
import com.google.gson.Gson;
import org.apache.http.impl.client.DefaultHttpClient;
import retrofit.http.GET;
import retrofit.http.GsonConverter;
import retrofit.http.RestAdapter;
import retrofit.http.Server;
import javax.inject.Named;
import java.util.List;
public class Client {
private static final String API_URL = "https://api.twitter.com/1/";
class Tweet {
String text;
}
interface Twitter {
@GET("statuses/user_timeline.json")
List<Tweet> tweets(@Named("screen_name") String user);
}
public static void main(String... args) {
// Create a very simple REST adapter which points the Twitter API endpoint.
RestAdapter restAdapter = new RestAdapter.Builder()
.setServer(new Server(API_URL))
.setClient(new DefaultHttpClient())
.setConverter(new GsonConverter(new Gson()))
.build();
// Create an instance of our Twitter API interface.
Twitter twitter = restAdapter.create(Twitter.class);
// Fetch and print a list of the 20 most recent tweets for a user.
List<Tweet> tweets = twitter.tweets("horse_ebooks");
for (Tweet tweet : tweets) {
System.out.println(tweet.text);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册