0
我覺得很蠢問這個和搜索similar questions,但我不明白爲什麼我能夠設置private String title
但無法得到它。Java - 無法獲得從本地方法設置的私人字符串
我有一個類,使得HTTP請求獲得一些JSON,解析它並設置實例變量從processResponse()
。當我在setTitle()
和getTitle()
中使用GWT.log()
時,控制檯顯示setTitle()
具有該值,但getTitle()
爲undefined
。
我正在使用GWT,如果這有所作爲。這個類將用於創建對象,這些變量將在整個程序中使用。我在這裏做錯了什麼?
public class Requester {
//other variables
private String title;
public Requester(UrlBuilder url) {
this.setURL(url);
this.generateRequest();
}
public void setURL(UrlBuilder url) {
this.url = url;
}
private void generateRequest() {
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url.getUrl()));
builder.setHeader("Authorization", authHeader);
try {
builder.sendRequest(null, new RequestCallback() {
// onError() method
public void onResponseReceived(Request request, Response response) {
processResponse(response);
}
});
} catch (RequestException e) {
//exception code
}
}
private void processResponse(Response response) {
//code to get title and assign it to String output
setTitle(output);
}
private void setTitle(String output) {
this.title = output;
}
public String getTitle() {
return this.title;
}
}
在'processResponse'方法,其中/如何你訪問變量'輸出',用作'setTitle'的參數? –
如果'setTitle'被調用並且設置了一個特定的值,並且後面的'getTitle'調用顯示爲'null',那麼它與'Requester'中的* instance *不同。調用代碼正在對另一個「請求者」對象進行調用。將日誌語句插入到構造函數中進行確認,然後查看進行調用的代碼,因爲問題是* there *,而不是此代碼中的代碼。 – Andreas
@ChrisSprague我爲這個賦值聲明瞭一個本地字符串 - 'String output = jsonArray.get(0).isObject()。get(「title」)。toString();' –