Initial commit
[mustard2:mustard2.git] / src / org / mustard2 / geonames / GeoNamesJSONUtil.java
1 /*
2  * MUSTARD: Android's Client for StatusNet
3  * 
4  * Copyright (C) 2009-2010 macno.org, Michele Azzolari
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  * 
20  */
21
22
23 package org.mustard2.geonames;
24
25 import org.json.JSONArray;
26 import org.json.JSONException;
27 import org.json.JSONObject;
28
29 public class GeoNamesJSONUtil {
30         
31         public static GeoName getGeoName(JSONObject o) {
32                 GeoName gn = null;
33                 if (o.has("geonames")) {
34                         try {
35                                 JSONArray geonames = o.getJSONArray("geonames");
36                                 if (geonames.length()>0) {
37                                         gn = new GeoName();
38                                         // I work only on the first one
39                                         JSONObject geoname = geonames.getJSONObject(0);
40                                         long id = geoname.getLong("geonameId");
41                                         gn.setGeonameId(id);
42                                         if (geoname.has("countryName"))
43                                                 gn.setCountryName(geoname.getString("countryName"));
44                                         
45                                         if (geoname.has("name"))
46                                                 gn.setName(geoname.getString("name"));
47                                         if (geoname.has("adminName1"))
48                                                 gn.setAdminName1(geoname.getString("adminName1"));
49                                         if (geoname.has("adminName2"))
50                                                 gn.setAdminName2(geoname.getString("adminName2"));
51                                         if (geoname.has("adminName3"))
52                                                 gn.setAdminName3(geoname.getString("adminName3"));
53                                         if (geoname.has("adminName4"))
54                                                 gn.setAdminName4(geoname.getString("adminName4"));
55                                         if (geoname.has("adminCode1"))
56                                                 gn.setAdminCode1(geoname.getString("adminCode1"));
57                                         if (geoname.has("adminCode2"))
58                                                 gn.setAdminCode2(geoname.getString("adminCode2"));
59                                         
60 //                                      Log.v("Mustard", "geonameId: " + id);
61                                         
62                                 }
63                         } catch (JSONException e) {
64                                 e.printStackTrace();
65                         }
66                 }
67                 return gn;
68         }
69
70 }