Java:为什么可变参数没有按预期在 Eclipse 中更新?

标签 java eclipse instantiation

根据我下面的代码,当我调试并到达 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);    
    }
  }
}

enter image description here

最佳答案

我找到了。创建原始 HttpURLConnection con 时,con.method 的默认值为 "GET"。但是,当为 me(在已运行的 vis 实例化内部创建的实例化)调用 con.setRequestMethod 时,它是 con.setRequestMethod("POST") 未反射(reflect)在 vis.me.con.method 中,而是反射(reflect)在 vis.me.con.delegate.method 中下一级.

关于Java:为什么可变参数没有按预期在 Eclipse 中更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27871933/

相关文章:

boolean 字段的 Java Bean 规范

eclipse - j2me 中的 HTTP/1.1 502 代理错误

PHP实例化子类

android - 从 Java 实例化 JNI 库的多个副本

java - 从 Java 程序创建 Mac OSX 包

java - 如何在Java小程序中检测浏览器和操作系统

java - Camel 2.10 更改 blueprint.xml 位置

java - 如何在 Eclipse 上导出带有图像的 jar?

java - 找不到共享库(java.lang.UnsatisfiedLinkError)

javascript - 如何实例化 JQuery 小部件 - 使用什么文本