I'm facing a very strange error I can't explain, maybe you can help me:
I'm working with JSON and I have a class for wrapping a response code in a JSON like {"code":"0"} :
public class Container {
int responseCode=0;
protected int _object_id=0;
public Container(String fromJSON){
_object_id=new Random().nextInt();
try {
setWithJSON(fromJSON);
} catch (Exception e) {
e.printStackTrace();
responseCode=-1;
}
}
protected JSONObject setWithJSON(String input) throws Exception{
JSONObject json = new JSONObject(input);
responseCode = json.getInt("code");
return json;
}
}
extending that class I have another class for wrapping the response code + an url: {"code":"0","url_tag":"http://good.url.com"}:
public class URLContainer extends Container {
private final String TAG="Test";
private String _url = "default_url";
public URLContainer(String fromJSON) {
super(fromJSON);
}
@Override
protected JSONObject setWithJSON(String input) throws Exception {
JSONObject json= super.setWithJSON(input);
_url=json.optString("url_tag", "no_url");
getUrl(); //Just for print the pointB
Log.e(TAG,"Point A ("+_object_id+"): url="+_url);
return json;
}
public String getUrl() {
Log.e(TAG,"Point B ("+_object_id+"): url="+_url);
return _url;
}
public void setUrl(String url) {
_url = url;
}
}
And the Activity class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String json="{\"code\":\"0\",\"url_tag\":\"http://good.url.com\"}";
URLContainer container = new URLContainer(json);
Log.e("adsads",""+container.getUrl());
}
and the resulting log is:
E/Test(20264): Point B (-569874754): url=http://good.url.com
E/Test(20264): Point A (-569874754): url=http://good.url.com
E/Test(20264): Point B (-569874754): url=default_url
E/Final(20264): default_url
Why does the final call return the default_url instead the good one?