我今天早些时候提出了一个类似的问题,并选择了一个答案,但我认为我没有正确地写出它来得到我需要的答案。请考虑下面的代码..在Java中使用相同的对象实例
public class ClassA {
private Main main;
Object obj = new Object;
public void setMain(Main main) {
this.main = main;
}
methodA() { //called first
obj.someFunction();
main.someFunction();
}
methodB() { //called second
obj.someOtherFunction();
}
}
对象“OBJ”在我的计划是,必须在整个过程中不使用“再创造”它(相同的散列,没有变化)的OAuthService实例。
我用methodA调用“obj”,调用main函数,然后再在methodB中调用“obj”。当我从methodA和methodB打印hashCode时,我得到了不同的结果,告诉我它正在“重新创建”实例。
我该如何避免这种情况?
我敢肯定,这听起来像是一个愚蠢的问题,更有经验的程序员,但我只是开始编程,我找不到我需要的任何解决方案。任何帮助将不胜感激。
实际代码
类 “AuthController”,其中 “OBJ” 是OAuth10aService “服务”,是了methodA “handlePinButton”,并且是的methodB “handlePinSubmit”。我已经尝试与其他两个OAuth库进行身份验证,并得到类似的错误。划线Java是我使用的是什么,现在,
public class AuthController {
private final String CONSUMER_KEY = "asdbfsdfbsdf";
private final String CONSUMER_SECRET = "asdfsdbvbvfsdsd";
final OAuth10aService service = new ServiceBuilder()
.apiKey(CONSUMER_KEY)
.apiSecret(CONSUMER_SECRET)
.build(TwitterApi.instance());
final Token requestToken = service.getRequestToken();
Desktop desktop = Desktop.getDesktop();
@FXML
public JFXTextField enterPinField;
@FXML
public JFXButton submitPinButton;
private Main main;
public void setMain(Main main) {
this.main = main;
}
public void handlePinButton() throws Exception { //call first
System.out.println(service.hashCode());
String authUrl = service.getAuthorizationUrl(requestToken);
desktop.browse(new URI(authUrl));
main.secondWindow();
}
public void handlePinSubmit() throws Exception { //call second
System.out.println(service.hashCode());
Verifier verifier = new Verifier(enterPinField.getText());
System.out.println(verifier);
Token accessToken = service.getAccessToken(requestToken, verifier);
System.out.println(accessToken);
}
}
输出如下:
WARNING: Loading FXML document with JavaFX API of version 8.0.65 by JavaFX runtime of version 8.0.51
722985119
Feb 12, 2016 4:47:05 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.65 by JavaFX runtime of version 8.0.51
960671619
[email protected]
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8390)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3758)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$166/1959217067.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:927)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1767)
... 46 more
Caused by: com.github.scribejava.core.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this: 'Error processing your OAuth request: Invalid oauth_verifier parameter'
at com.github.scribejava.core.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:39)
at com.github.scribejava.core.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:29)
at com.github.scribejava.core.oauth.OAuth10aService.getAccessToken(OAuth10aService.java:86)
at controller.AuthController.handlePinSubmit(AuthController.java:65)
... 56 more
可以显示为'someFunction'和'someOtherFunction'相关的代码?很难根据你的描述找出问题的根源 –
是的,我刚刚添加了它。我尝试了两个不同的OAuth库,得到类似的错误,所以我猜测问题不在Scribe-java中,而是在会话实例中。 – Shane