Java异常的变量行为

时间:2014-07-17 16:43:40

标签: java android

我有这样的类(实际上onResponse方法解析JSON,但我为了简单起见删除了那段od代码):

class DistRespListener implements HttpClient.HttpResponseListener {

private double dist;

@Override
public void onResponse(HttpMetaResponse response) {
    Double distTemp = 14.7;
    setDist(distTemp);
    Log.d("distanceInner2", String.valueOf(this.dist));
}

public void setDist(double dist) {
    this.dist = dist;
    Log.d("distanceInner4", String.valueOf(this.dist));
}
public double getDist() {
    Log.d("distanceInner3", String.valueOf(this.dist));
    return this.dist;
}

稍后在代码中,我做了这个:

HttpClient httpClient = new HttpClient();
DistRespListener distRespListener = new DistRespListener();
httpClient.executeRequestUsingOwnListener(new HttpGet(builder.build().toString()), distRespListener);

Log.d("distanceOuter", String.valueOf(distRespListener.getDist()));

httpClient对象的方法executeRequestUsingOwnListener调用distRespListener对象的onResponse()方法。

问题是,getDist()方法总是返回" 0.0"。

1 个答案:

答案 0 :(得分:0)

检查日志消息的显示顺序 - 它将是distanceinner3,distanceouter,distanceinner2和distanceinner4。这告诉我们在收到响应之前记录前两个值,而后两个值记录在之后。这就是为什么你总是看到前两个0。