Created
March 11, 2014 00:08
-
-
Save swankjesse/9477082 to your computer and use it in GitHub Desktop.
Twitter hello world with OkHttp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.squareup.okhttp.sample; | |
import com.squareup.okhttp.ConnectionPool; | |
import com.squareup.okhttp.OkHttpClient; | |
import java.lang.reflect.Field; | |
import twitter4j.Paging; | |
import twitter4j.ResponseList; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterFactory; | |
import twitter4j.auth.AccessToken; | |
import twitter4j.internal.http.HttpClientWrapper; | |
import twitter4j.internal.http.alternative.HttpClientImpl; | |
public class TwitterSample { | |
public static void main(String[] args) throws Exception { | |
String consumerKey = ""; | |
String consumerSecret = ""; | |
String accessToken = ""; | |
String accessTokenSecret = ""; | |
int count = Integer.parseInt("500"); | |
Twitter twitter = TwitterFactory.getSingleton(); | |
twitter.setOAuthConsumer(consumerKey, consumerSecret); | |
twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret)); | |
StringBuilder sb = new StringBuilder(); | |
sb.append("java: " + System.getProperty("java.runtime.version") + " " | |
+ System.getProperty("java.version") + " " + System.getProperty("java.vm.version") + "\n"); | |
sb.append("start\n"); | |
HttpClientImpl.sPreferSpdy = true; | |
HttpClientImpl.sPreferHttp2 = true; | |
long startTick = System.currentTimeMillis(); | |
Paging paging = new Paging(); | |
paging.setCount(count); | |
ResponseList<twitter4j.Status> result = twitter.getHomeTimeline(paging); | |
long elapsed = System.currentTimeMillis() - startTick; | |
sb.append(" elapsed: " + elapsed + "ms\n"); | |
// dump | |
sb.append(" " + result.size() + " results\n"); | |
int i = 0; | |
for (twitter4j.Status s : result) { | |
String t = s.getText(); | |
sb.append(" [" + s.getUser().getScreenName() + "][" + (t.length() <= 10 ? t : t.substring(0, 10)) + "]\n"); | |
} | |
// SPDY,HTTP/2.0 info | |
sb.append("\n"); | |
HttpClientImpl http = getHttpClientImpl(twitter); | |
ConnectionPool p = getSpdyConnectionPool(http); | |
if (p == null) { | |
sb.append("HTTP/2.0 : Disabled\n"); | |
} else { | |
sb.append("HTTP/2.0 Connections: [" + p.getSpdyConnectionCount() + "/" + p.getConnectionCount() + "]\n"); | |
sb.append("Protocol: [" + http.getLastRequestProtocol() + "]\n"); | |
} | |
System.out.println(sb); | |
} | |
public static ConnectionPool getSpdyConnectionPool(HttpClientImpl http) throws Exception { | |
Field f3 = http.getClass().getDeclaredField("client"); | |
f3.setAccessible(true); | |
// client = http.client | |
OkHttpClient client = (OkHttpClient) f3.get(http); | |
if (client == null) return null; | |
return client.getConnectionPool(); | |
} | |
public static HttpClientImpl getHttpClientImpl(Twitter twitter) throws Exception { | |
Class<?> clazz = Class.forName("twitter4j.TwitterBaseImpl"); | |
Field f1 = clazz.getDeclaredField("http"); | |
f1.setAccessible(true); | |
// wrapper = twitter.http | |
HttpClientWrapper wrapper = (HttpClientWrapper) f1.get(twitter); | |
Field f2 = HttpClientWrapper.class.getDeclaredField("http"); | |
f2.setAccessible(true); | |
// http = wrapper.http | |
return (HttpClientImpl) f2.get(wrapper); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(This code is from a bug report against OkHttp)