I'm developing an Android app to analyze the UP Band data. Using the example given in the UP SDK for Android , at the moment I can get the trend data as a JSON object. The problem is that this JSON object is malformed. In fact when I try to deserialize the Trend data with:
JsonElement je = new JsonParser().parse(o.toString());
I get the following error:
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected value at line ...
Moreover, inspecting the result of the API call I get:
{meta={user_xid=MASKED, message=OK, code=200.0, time=1.430319261E9}, data={earliest=2.0130408E7, data=[[2.0150423E7, {e_protein=null, weight=78.0, goal_body_weight_intent=null, body_fat=null, m_distance=8073.0, s_awakenings=null, height=1.72, m_lcat=1336.0, goal_body_weight=null, s_quality=null, m_steps=10760.0, e_cholesterol=null, s_light=null, e_sat_fat=null, n_bedtime=null, m_workout_time=0.0, e_calcium=null, s_bedtime=null, n_awakenings=null, n_light=null, s_awake_time=null, pal=null, n_duration=null, m_lcit=9660.0, m_active_time=5717.0, e_unsat_fat=null, m_calories=532.710151311, e_calories=null, bmr=1426.58256449, m_total_calories=1959.2927158, n_sound=null, e_sugar=null, e_sodium=null, s_awake=null, s_asleep_time=null, s_duration=null, n_awake=null, age=69.8273972603, e_carbs=null, e_fiber=null, s_sound=null, n_quality=null, n_awake_time=null, gender=0.0, n_asleep_time=null}], [2.0150424E7, {e_protein=null, weight=78.0, goal_body_weight_intent=null, body_fat=null, m_distance=9641.0, s_awakenings=null, height=1.72, m_lcat=1145.0, goal_body_weight=null, s_quality=null, m_steps=12657.0, e_cholesterol=null, s_light=null, e_sat_fat=null, n_bedtime=null, m_workout_time=0.0, e_calcium=null, s_bedtime=null, n_awakenings=null, n_light=null, s_awake_time=null, pal=null, n_duration=null, m_lcit=11460.0, m_active_time=6508.0, e_unsat_fat=null, m_calories=632.988689047, e_calories=null, bmr=1412.57952142, m_total_calories=2045.56821047, n_sound=null, e_sugar=null, e_sodium=null, s_awake=null, s_asleep_time=null, s_duration=null, n_awake=null, age=69.8301369863, e_carbs=null, e_fiber=null, s_sound=null, n_quality=null, n_awake_time=null, gender=0.0, n_asleep_time=null}], [2.0150425E7, {e_protein=null, weight=78.0, goal_body_weight_intent=null, body_fat=null, m_distance=21643.0, s_awakenings=null, height=1.72, m_lcat=2455.0, goal_body_weight=null, s_quality=null, e_calories=null, e_cholesterol=null, s_light=null, e_sat_fat=null, n_bedtime=null, m_workout_time=0.0, e_calcium=null, s_bedtime=null, n_awakenings=null, n_light=null, s_awake_time=null, s_sound=null, pal=null, n_duration=null, m_lcit=1860.0, m_active_time=14625.0, e_unsat_fat=null, m_calories=1422.11015205, bmr=1269.04654879, m_total_calories=2691.15670084, n_sound=null, s_asleep_time=null, e_sodium=null, s_awake=null, e_sugar=null, s_duration=null, n_awake=null, age=69.8328767123, e_carbs=null, e_fiber=null, m_steps=28322.0, n_quality=null, n_awake_time=null, gender=0.0, n_asleep_time=null}], [2.0150426E7, {e_protein=null, weight=78.0, goal_body_weight_intent=null, body_fat=null, m_distance=1572.0, s_awakenings=null, height=1.72, m_lcat=265.0, goal_body_weight=null, s_quality=null, m_steps=2116.0, e_cholesterol=null, s_light=null, e_sat_fat=null, n_bedtime=null, m_workout_time=0.0, e_calcium=null, s_bedtime=null, n_awakenings=null, n_light=null, e_calories=null, pal=null, n_duration=null, m_lcit=6360.0, s_awake_time=null, e_unsat_fat=null, m_calories=104.843981181, bmr=1507.45521348, m_total_calories=1612.29919466, n_sound=null, s_asleep_time=null, e_sodium=null, s_awake=null, e_sugar=null, s_duration=null, n_awake=null, age=69.8356164384, e_carbs=null, e_fiber=null, s_sound=null, n_quality=null, n_awake_time=null, gender=0.0, n_asleep_time=null, m_active_time=1140.0}], [2.0150427E7, {e_protein=null, weight=78.0, goal_body_weight_intent=null, body_fat=null, m_distance=4334.0, s_awakenings=0.0, height=1.72, m_lcat=1091.0, goal_body_weight=null, s_quality=0.0, m_steps=5726.0, e_cholesterol=null, s_light=0.0, e_sat_fat=null, n_bedtime=null, m_workout_time=0.0, e_calcium=null, s_bedtime=-5400.0, n_awakenings=null, n_light=null, s_awake_time=17100.0, pal=null, n_duration=null, m_lcit=4320.0, m_active_time=2978.0, e_unsat_fat=null, m_calories=283.00944155, e_calories=null, bmr=1474.94024527, m_total_calories=1757.94968682, n_sound=null, s_asleep_time=-5400.0, e_sodium=null, s_awake=0.0, e_sugar=null, s_duration=22500.0, n_awake=null, age=69.8383561644, e_carbs=null, e_fiber=null, s_sound=0.0, n_quality=null, n_awake_time=null, gender=0.0, n_asleep_time=null}], [2.0150428E7, {e_protein=null, weight=78.0, goal_body_weight_intent=null, body_fat=null, m_distance=null, s_awakenings=null, height=1.72, m_lcat=null, goal_body_weight=null, s_quality=null, e_calories=null, e_cholesterol=null, s_light=null, e_sat_fat=null, n_bedtime=null, m_workout_time=null, e_calcium=null, s_bedtime=null, n_awakenings=null, n_light=null, m_steps=null, pal=null, n_duration=null, m_lcit=null, s_awake_time=null, e_unsat_fat=null, m_calories=null, bmr=1527.57418082, m_total_calories=1527.57418082, n_sound=null, s_asleep_time=null, e_sodium=null, s_awake=null, e_sugar=null, s_duration=null, n_awake=null, age=69.8410958904, e_carbs=null, e_fiber=null, s_sound=null, n_quality=null, n_awake_time=null, gender=0.0, n_asleep_time=null, m_active_time=null}], [2.0150429E7, {e_protein=null, weight=78.0, goal_body_weight_intent=null, body_fat=null, m_distance=null, s_awakenings=null, height=1.72, m_lcat=null, goal_body_weight=null, s_quality=null, m_steps=null, e_cholesterol=null, s_light=null, e_sat_fat=null, n_bedtime=null, m_workout_time=null, e_calcium=null, s_bedtime=null, n_awakenings=null, n_light=null, s_awake_time=null, pal=null, n_duration=null, m_lcit=null, m_active_time=null, e_unsat_fat=null, m_calories=null, e_calories=null, bmr=1527.55567397, m_total_calories=503.191690242, n_sound=null, s_asleep_time=null, e_sodium=null, s_awake=null, e_sugar=null, s_duration=null, gender=0.0, age=69.8438356164, e_carbs=null, e_fiber=null, s_sound=null, n_quality=null, n_awake_time=null, n_awake=null, n_asleep_time=null}]], links={next=/nudge/api/v.1.1/users/ymQeH-UUAU8hs54Ad7Mxjw/trends/?end_date=20150422&bucket_size=d&num_buckets=7}}}
The main problem in the previous JSON object is that the keys aren't well formed. In fact they should be strings of characters between "". Could you please give me some indication on how to solve this problem? Thanks a lot for your help. [EDIT]
This the callback function I was starting to modify to get the data from Jawbone:
private Callback genericCallbackListener = new Callback<Object>() {
@Override
public void success(Object o, Response response) {
Log.e(LOG_TAG, "api call successful, json output: " + o.toString());
//I added the following 3 lines of code to parse the JSON result
JsonElement je = new JsonParser().parse(o.toString());
JsonObject root = je.getAsJsonObject();
JsonElement je2 = root.get("Data");
Toast.makeText(getApplicationContext(), je2.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError retrofitError) {
Log.e(LOG_TAG, "api call failed, error message: " + retrofitError.getMessage());
Toast.makeText(getApplicationContext(), retrofitError.getMessage(), Toast.LENGTH_LONG).show();
}
};
ok I believe I know what your problem is. You are using retrofit Callback
wrong.
You do not need to parse the result. Jawbone + retrofit will do it for you.
Check this example:
public interface BookService{
@GET("/books")
void getBooks(Callback<List<Book>> books);
}
// AND THEN
private Callback genericCallbackListener = new Callback<List<Book>>() {
@Override
public void success(List<Book> books, Response response) {
[...] // use books
}
@Override
public void failure(RetrofitError retrofitError) {
}
}
taskService.getBooks(genericCallbackListener);
So do not try manually to parse the response. Let Jawbone Android SDK do it for you.