Wouter125 Wouter125 - 2 months ago 9
Android Question

Retrofit with Halarious and Teleport.org

I'm working on a small android app that uses the teleport.org API to list the urban areas. To do so, I'm using the following response URL:

https://api.teleport.org/api/urban_areas/

The response looks like this:

{
"_links": {
"curies": [
{
"href": "https://developers.teleport.org/api/resources/Location/#!/relations/{rel}/",
"name": "location",
"templated": true
}
],
"self": {
"href": "https://api.teleport.org/api/urban_areas/"
},
"ua:item": [
{
"href": "https://api.teleport.org/api/urban_areas/teleport:u173z/",
"name": "Amsterdam"
}
]
},
"count": 153
}


What this gives me is a json file in HAL format. Since I'm using retrofit to load my API I need a custom converter to get this info stored in the POJO classes. I picked Halarious for this. http://halarious.ch/

I've set it up like this.

MainActivity:

String url = "https://api.teleport.org/api/";
TextView txt_city;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
txt_city = (TextView) findViewById(R.id.txt_city);

setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

getCities();

}

void getCities() {
//Creating Rest Services
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(HALConverterFactory.create(CityData.class))
.build();

RestInterface service = retrofit.create(RestInterface.class);

Call<CityData> call = service.getCityList();


call.enqueue(new Callback<CityData>() {
@Override
public void onResponse(Call<CityData> call, Response<CityData> response) {
try {
String city = response.body().getUaItem().get(0).getName();
//Log.e("stad", city);

txt_city.setText("city 12 : " + city);

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

@Override
public void onFailure(Call<CityData> call, Throwable t) {
// Log error here since request failed
}
});
}


HALConverterFactory.class:

public final class HALConverterFactory extends Converter.Factory {
private final Gson gson;

public static HALConverterFactory create(Class<?> type) {
return new HALConverterFactory(type);
}

private HALConverterFactory(Class<?> type) {
if (!HalResource.class.isAssignableFrom(type))
throw new NullPointerException("Type should be a subclass of HalResource");
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(HalResource.class, new HalSerializer());
builder.registerTypeAdapter(HalResource.class, new HalDeserializer(type));
builder.setExclusionStrategies(new HalExclusionStrategy());
this.gson = builder.create();
}

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new HALResponseBodyConverter<>(gson);
}

@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
return super.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
}
}


HALResponseBodyConverter.class:

final class HALResponseBodyConverter<T extends HalResource> implements Converter<ResponseBody, T>{

private final Gson gson;

HALResponseBodyConverter(Gson gson) {
this.gson = gson;
}

@Override public T convert(ResponseBody value) throws IOException {
BufferedSource source = value.source();
try {
String s = source.readString(Charset.forName("UTF-8"));
return (T) gson.fromJson(s, HalResource.class);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
closeQuietly(source);
}
}

private static void closeQuietly(Closeable closeable) {
if (closeable == null) return;
try {
closeable.close();
} catch (IOException ignored) {
}
}
}


And I'm having the following POJO classes.

CityData:

public class CityData implements HalResource {

@HalLink
private List<Cury> curies = new ArrayList<Cury>();
private Self self;
private List<UaItem> uaItem = new ArrayList<UaItem>();
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

private Integer count;

//getters and setters
}


UaItem:

public class UaItem implements HalResource {
@HalLink
private String href;
private String name;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
//getters and setters
}


What I wanna do now is retrieving the following:

String city = response.body().getUaItem().get(0).getName();


But the problem is that my getUaItem is completely empty. It just returns []. So get(0) is useless since the array is empty. I can get the count tho, since that's on a top level, but the UaItem is causing me problems. Why is this?

Kind regards,

Wouter

Answer

I think this is caused by the fact that the original response variable name is ua:Item, not uaItem. Probably the automatic mapping isn't working here. You could fix it by explicitly writing out the variable name:

@serializedName("ua:item") private List<UaItem> uaItem = new ArrayList<UaItem>();
Comments