我今天早些時候提出了一個類似的問題,並選擇了一個答案,但我認爲我沒有正確地寫出它來得到我需要的答案。請考慮下面的代碼..在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