簡體   English   中英

Java:為什么變量參數沒有按Eclipse中預期的那樣更新?

[英]Java: Why aren't variable parameters updating as expected in Eclipse?

基於下面我的代碼,當我調試,我得到的一點me (jurl實例),其中con.setRequestMethod(method)被調用時,變量選項卡第一個展示:

Name            Value
-----------     ----------------------------------------        
this            jurl (id=128)
  -con          HttpsURLConnectionImpl (id=129)
    -method     "GET" (id=116)

method          "POST" (id=118)
furl            "https://www.someurl.com/login" (id=117)

如果我正確理解的話,那么con.setRequestMethod("POST")不應將this.con.method(id 116)從“ GET”更改為“ POST”嗎? 因為不是。 一旦調試通過con.setRequestMethod(method) ,this.con.method仍然顯示“ GET”。 因此,或者我在這里丟失了某些東西,或者Eclipse的變量跟蹤器已被調試。

View.java

public Class View {
  private AccountManager accountManager = new AccountManager(this);

  private void login() {
    String username = textuser.getText();
    String password = textpass.getText();
    String locid = getLocId();
    Hashtable result = null;

    result = accountManager.login(username, password, locid);
  }  
}

AccountManager.java

public class AccountManager {
  public View Parent = null;
  private String USERNAME = null;
  private String PASSWORD = null;
  private String locid = null;

  public View Parent = null;

  public AccountManager(View view) {
    this.Parent=view;
    this._locationAssistant = l;
  }

  public Hashtable login(String username, String password, String locid) {
    final String USERNAME = username;
    final String PASSWORD = password;
    this.USERNAME = username;
    this.PASSWORD = password;
    Hashtable result = new Hashtable();

    URLVisitor vis = new URLVisitor();
    vis.setURL("https://www.someurl.com/login");
    vis.setMethod("POST");
    vis.execute();
  }
}

URLVisitor.java

public class URLVisitor {
  private String method= "GET";
  private String url;
  public jurl me = null;

  public URLVisitor() {
  }

  public void setURL(String url) {
    this.url = url;
  }

  public void setMethod(String method) {
    this.method = method;
  }

  public void execute() {
    me = new jurl(this.method, this.url);
  }
}

jurl.java

public class jurl {
  private URL url;
  private HttpURLConnection con = null;

  public jurl(String method, String furl) {
    try {
      url new URL(furl);
      con = (HttpURLConnection) url.openConnection();
      con.setRequestMethod(method);    
    }
  }
}

在此處輸入圖片說明

我找到了。 創建原始HttpURLConnection concon.method的默認值為"GET" 但是,對mecon.setRequestMethod是在已經運行的vis實例內部創建的實例時,它的con.setRequestMethod("POST")不會反映在vis.me.con.method ,而是一個vis.me.con.method 。在vis.me.con.delegate.methodvis.me.con.delegate.method

一旦調試通過con.setRequestMethod(method),this.con.method仍會顯示“ GET”。

你不能指望實地改變,直到 setRequestMethod調用返回。 在我看來,調試器的行為正常/正確。

我也可能誤解了您,而您實際上看到的是該字段的過時價值。 嘗試讓調試器從字段中重新獲取值。


但這不應該影響代碼的實際行為方式,無論您是否通過調試器運行它。

最后,請注意,您的代碼實際上並未調用connect或嘗試查看狀態或內容,因此method字段的狀態是學術性的。 它不會被使用...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM