0

I'm new on java/android:

I'm using this list of objects on wp7 and I want pass to android, how I do this:

My big object c#:

public class ListCountries
{
    public List<CountriesRepresented> _countriesRepresented { get; set; }
    public List<CountriesOrigin > _countriesOrigin { get; set; }
}

My others two objects in c#:

public class CountriesRepresented
{
    public int CountryID { get; set; }
    public string Designation { get; set; }
    public string Symbol { get; set; }
    public string NomDesignationISO { get; set; }
}

public class CountriesOrigin 
{
    public int CountryID { get; set; }
    public string Designation { get; set; }
    public string Symbol { get; set; }
    public string NomDesignationISO { get; set; }
}

My java Deserializer:

public Object[] getListCountries()
{

     try {
        HttpClient httpClient = new DefaultHttpClient();

        HttpPost post = new HttpPost(Config.WS_PATH);
        post.setHeader("content-type", "application/json; charset=UTF-8");
        post.addHeader("Client-Application","3601cfde-e440-4a84-a2cc-a402f4c7bd14");        
       HttpResponse resp = httpClient.execute(post);
       String respStr = EntityUtils.toString(resp.getEntity());

        ListCountries _listCountries = new JSONDeserializer().deserialize(ListCountries .class, respStr);

        return _listCountries;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

My big object in java:

public class ListCountries {
public List<CountriesRepresented> _CountriesRepresented;
public List<CountriesOrigin > _CountriesOrigin ;

public List<CountriesRepresented> getCountriesRepresented() {  
    return this._CountriesRepresented;  
  }  

  public List<CountriesOrigin > getCountriesOrigin() {  
    return this._CountriesOrigin ;  
  }


  public void setCountriesRepresented (List<CountriesRepresented> CountriesRepresented) {  
    this._CountriesRepresented = CountriesRepresented;  
  } 
  public void setCountriesOrigin  (List<CountriesOrigin > CountriesOrigin ) {  
    this._CountriesOrigin  = CountriesOrigin ;  
  }

}

My service is on WebAPI and give me an correct answer example: `{"PaisesRepresentantes":[{"PaisID":4,"Designacao":"Alemanha","Sigla":"DEU","NomDesignacaoISO":"GERMANY"},{"PaisID":21,.......

r-magalhaes
  • 427
  • 2
  • 9
  • 18
  • What is the result, did you get any exception? If yes tell us the exception. – kostas ch. Dec 30 '13 at 12:09
  • Try this post http://stackoverflow.com/questions/16590377/custom-json-deserializer-using-gson – kostas ch. Dec 30 '13 at 12:11
  • My _listCountries is null, i think my get;set; in java is wrong – r-magalhaes Dec 30 '13 at 12:12
  • 1
    As far as i can tell by seeing you code the JSON that you get contains a class with different name and with different column names Like you class name is "CountriesRepresented" and the JSON is returning "PaisesRepresentantes". you need to make all the class names and the variable name same for Deserializing the JSON to C# class. you can create the class of your JSON result from this URL: [json2csharp](http://json2csharp.com/) and then use the generated class in the Deserializer you created – Nish Rathi Dec 30 '13 at 12:15
  • nice link Nish, but you know something but for java :) – r-magalhaes Dec 30 '13 at 12:19
  • I change my code usign GSON, i works finally :) very thanks to all – r-magalhaes Dec 30 '13 at 17:59
  • I find a site like json2csharp but for java http://www.jsonschema2pojo.org/ – r-magalhaes Jan 13 '14 at 15:23

1 Answers1

1

your classes in Java should be something like,

public class ListCountries
{
    public List<CountriesRepresented> _countriesRepresented; //Make it private if you want and then you can add getter setter function
    public List<CountriesOrigin > _countriesOrigin;
}

public class CountriesRepresented
{
    public int CountryID; //Make it private if you want and then you can add getter setter function
    public String Designation;
    public String Symbol;
    public String NomDesignationISO;
}

public class CountriesOrigin 
{
    public int CountryID; //Make it private if you want and then you can add getter setter function
    public String Designation;
    public String Symbol;
    public String NomDesignationISO;
}

The problem with your existing code is that you have declared variable as _CountriesRepresented rather it should be _countriesRepresented i.e. properties are case sensitive and those are mapped to variables declared in class and do remember to add a default constructor if you add any custom constructor to any of those classes

Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58