0

我正在尝试使用 Twitter4j 库在 Google App Engine 上创建 Java 应用程序。

当我创建普通(不是谷歌)应用程序时,我可以使用 twitter4j 并且一切正常。我在项目 -> 属性 -> 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)
4

3 回答 3

1

Twitter4j 的 JAR 应该放在 WEB-INF/lib 中。

于 2013-08-23T18:29:04.100 回答
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)
...
...
于 2013-08-24T21:36:20.363 回答
0

GAE 中的Twitter4j 异步直接调用new Thread()不允许的。

您需要修改DispatcherImpl构造函数以使用 GAEThreadManager来创建线程。

请参考GAE 沙盒约束

于 2013-08-26T18:50:33.197 回答