0

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?

Addev
  • 31,819
  • 51
  • 183
  • 302

2 Answers2

2

It's because of your super(fromJSON); in URLContainer's constructor. What happends is

  • call to URLContainer's constructor
  • call to Container's constructor
  • call to URLContainer's setWithJSON(String input) //_url is set to your desired value in the returned JSON object
  • call to Container's constructor // _url is NOT set in a NEWLY created JSON object

Just make the call to setWithJSON(String input) on an instance of URLContainer AFTER that instance was created.

Shivan Dragon
  • 15,004
  • 9
  • 62
  • 103
1

This will work

        .......
        public Container(String fromJSON){
          _object_id=new Random().nextInt();
          //            try {
          //                setWithJSON(fromJSON);
          //            } catch (Exception e) {
          //                e.printStackTrace();
          //                responseCode=-1;
          //            }
        }
        .......

In activity:

       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);
          container.setWithJSON(json);   
          Log.e("adsads",""+container.getUrl());
       }
Vyacheslav Shylkin
  • 9,741
  • 5
  • 39
  • 34
  • Yep, but I'd like to know why it doesn't work. Thanks for your answer – Addev Feb 27 '12 at 12:26
  • My English vary bad - sory. Check creation of URLContainer - first create Container, in constructor of Container called setWithJson(overrided), set _url ..., after constructor Conteiner finished - begin initializition fields of UrlContainer - and you have _url = "default_url" – Vyacheslav Shylkin Feb 27 '12 at 12:35