Created
March 23, 2017 14:58
-
-
Save cutiko/7b2ba39d448ea604495a2e34397c102e to your computer and use it in GitHub Desktop.
JsonObject example using Retrofit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LocationByAddress extends AsyncTask<String, Void, String[]> { | |
private LocationCallback callback; | |
public LocationByAddress(LocationCallback callback) { | |
this.callback = callback; | |
} | |
@Override | |
protected String[] doInBackground(String... params) { | |
//TODO this vallidation is pointless we have to make sure the texts are not empty, this texts come from and edit text, so worst case scenario is empty text "" | |
if (params.length > 0) { | |
String address = ""; | |
for (String param : params) { | |
address = param+", "; | |
} | |
GeoGet geoGet = new GeoInterceptor().getInterceptor(); | |
Map<String, String> queryMap = new HashMap<>(); | |
queryMap.put("address", address); | |
queryMap.put("sensor", "true"); | |
Call<JsonObject> jsonObjectCall = geoGet.get(queryMap); | |
try { | |
Response<JsonObject> response = jsonObjectCall.execute(); | |
JsonObject mainObject = response.body(); | |
if (response.isSuccessful() && mainObject.get("status").toString().equals("\"OK\"")) { | |
JsonArray results = mainObject.getAsJsonArray("results"); | |
JsonObject innerResult = (JsonObject) results.get(0); | |
JsonObject geometry = (JsonObject) innerResult.get("geometry"); | |
JsonObject location = (JsonObject) geometry.get("location"); | |
String lat = location.get("lat").toString(); | |
String lng = location.get("lng").toString(); | |
String[] coordinates = {lat, lng}; | |
return coordinates; | |
} else { | |
callback.fail(); | |
return null; | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
callback.fail(); | |
return null; | |
} | |
} else { | |
callback.noAddress(); | |
return null; | |
} | |
} | |
@Override | |
protected void onPostExecute(String[] strings) { | |
if (strings != null) { | |
callback.success(strings[0], strings[1]); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GeoInterceptor { | |
private static final String BASE_URL = "http://maps.googleapis.com/maps/"; | |
public GeoGet getInterceptor() { | |
Retrofit interceptor = new Retrofit.Builder() | |
.baseUrl(BASE_URL) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build(); | |
GeoGet geoGet = interceptor.create(GeoGet.class); | |
return geoGet; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface GeoGet { | |
@GET("api/geocode/json") | |
Call<JsonObject> get(@QueryMap Map<String, String> map); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment