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