1

我正在开发一个 Android 应用程序来分析 UP Band 数据。使用 UP SDK for Android 中给出的示例,目前我可以将趋势数据作为 JSON 对象获取。问题是这个 JSON 对象格式不正确。事实上,当我尝试反序列化趋势数据时:

JsonElement je = new JsonParser().parse(o.toString());

我收到以下错误:

com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:行中的预期值...

此外,检查 API 调用的结果,我得到:

{meta={user_xid=屏蔽, 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_18calories=1426.58256449,m_total_18calories= , 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,性别=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_time=null, e_sodium_asleep_ake=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=空,重量=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.8328,76 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. 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,目标体重=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,年龄=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 , 目标体重=空, 身体脂肪=空, m_距离=空, s_awakenings=空, 身高=1.72, m_lcat=空, 目标体重=空, s_quality=空, m_steps=空, e_cholesterol=空, s_light=空, e_sat_fat=空, 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, 性别=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} }}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=空,n_awake=null,n_asleep_time=null}]],链接={next=/nudge/api/v.1.1/users/ymQeH-UUAU8hs54Ad7Mxjw/trends/?end_date=20150422&bucket_size=d&num_buckets=7}}}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=空,n_awake=null,n_asleep_time=null}]],链接={next=/nudge/api/v.1.1/users/ymQeH-UUAU8hs54Ad7Mxjw/trends/?end_date=20150422&bucket_size=d&num_buckets=7}}}

前一个 JSON 对象的主要问题是键的格式不正确。实际上它们应该是“”之间的字符串。你能给我一些关于如何解决这个问题的指示吗?非常感谢你的帮助。[编辑]

这是我开始修改以从 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();
}

};

4

1 回答 1

1

好的,我相信我知道您的问题是什么。您正在使用改造Callback错误。

您不需要解析结果。Jawbone +改装将为您完成。

检查这个例子:

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);

所以不要尝试手动解析响应。让Jawbone Android SDK为您完成。

于 2015-05-04T10:48:35.173 回答