我正在尝试使用Twitter4j库在Google App Engine上创建Java应用程序。
当我创建普通(不是谷歌)应用程序时,我可以使用twitter4j,一切正常。我在Project中包含了库 - >属性 - > Java构建路径 - >图书馆 - >添加外部JAR。 我的代码:
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumer_key, consumer_secret);
twitter.setOAuthAccessToken(new AccessToken(oauth_token,oauth_token_secret));
try{
ResponseList<Status> a = twitter.getUserTimeline("medium");
for(Status b:a)
{
System.out.println(b.getText());
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
当我创建google java项目并尝试初始化Twitter时,我收到错误:
java.lang.NoClassDefFoundError: twitter4j/TwitterFactory
at com.paperbox.HelloWorldServlet.doGet(HelloWorldServlet.java:26)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:368)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:351)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:97)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:485)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
答案 0 :(得分:1)
Twitter4j的JAR应放在WEB-INF / lib。
中答案 1 :(得分:0)
我不知道Twitter4j是否存在问题 - GAE连接或其他问题。
现在我必须使用异步连接 - 代码:
AsyncTwitterFactory factory = new AsyncTwitterFactory();
AsyncTwitter asyncTwitter = factory.getInstance();
asyncTwitter.setOAuthConsumer(consumer_key, consumer_secret);
asyncTwitter.setOAuthAccessToken(new AccessToken(oauth_token,oauth_token_secret));
asyncTwitter.addListener(listener);
//asyncTwitter.updateStatus("asd");
System.out.println(asyncTwitter.toString());
try{
asyncTwitter.getUserTimeline("medium");
}
catch(Exception e)
{
System.out.println(e.toString());
}
例外:
HTTP ERROR 500
Problem accessing /testapp Reason:
java.lang.reflect.InvocationTargetException
Caused by:
java.lang.AssertionError: java.lang.reflect.InvocationTargetException
at twitter4j.internal.async.DispatcherFactory.getInstance(DispatcherFactory.java:61)
at twitter4j.AsyncTwitterImpl.getDispatcher(AsyncTwitterImpl.java:3146)
at twitter4j.AsyncTwitterImpl.getUserTimeline(AsyncTwitterImpl.java:109)
at twitter4j.AsyncTwitterImpl.getUserTimeline(AsyncTwitterImpl.java:170)
...
...
答案 2 :(得分:0)
Twitter4j异步直接在GAE中调用new Thread()
NOT 允许。
您需要修改DispatcherImpl
构造函数以使用GAE的ThreadManager
来创建线程。