1

背景:

我正在使用通用 HTTPebble 库为 Pebble Smartwatch 编写一些东西,以充当手表和手机之间的桥梁。这需要一个特定的 iOS 应用程序和一个不同的 Android 应用程序,尽管它们都使用相同的框架和库。

问题:

我能够对 PHP 服务器进行 HTTP 调用,并成功返回和解码来自 Android Gingerbread 的 JSON 连音组结果。但是,在 iOS 设备上使用相同的调用时,我无法收到有效的预期结果。

这是我用来接收 JSON 结果的代码:

#define INTRO_DAY1_ICON 1
Tuple*   icon    = dict_find(received,INTRO_DAY1_ICON);
if (icon){
    dayTransition.day[0].currentWeather = icon->value->int8;

我期望 icon->value->int8 是 0、1、2、3、11 或 99 的整数值。在 Android 上,这目前正在按预期工作。但是,在 iOS 上,我似乎总是返回 8 的结果。更进一步,该图标通过 Android 返回 > 50,000(有数据)的值,而在 iOS 上返回值 0。

更令人费解的是,提供的示例代码能够在两个设备上返回有效结果,但用我的代码镜像该功能并不会产生相同的结果。(意味着当从任一设备调用时 data_tuple 被填充)该代码如下:

    Tuple* data_tuple = dict_find(received, WEATHER_KEY_CURRENT);
    if(data_tuple) {
         uint16_t value = data_tuple->value->int16;
         uint8_t icon = value >> 11;
         if(icon < 10) {

关于为什么会发生这种情况的任何想法?

4

1 回答 1

0

I finally figured it out! I believe that the issue was how the data is interpreted on iOS vs Android. On Android, I could just pass back any sort of integer (signed or unsigned, 8, 16, or 32 bit) and it would just seem to work out. However, on iOS, I needed to do a bitwise shifting process on the JSON Output (before encoding) to make it into an unsigned 16 bit integer. Then once it got back to the Pebble, there is code to shift that unsigned 16 bit integer into an unsigned 8 bit integer, before assigning it to a standard c int

uint16_t value1 = icon_tuple1->value->int16;
uint8_t icon1 = value1 >> 11;
dayTransition.day[0].currentWeather = icon1;

TL;DR Shift your JSON >> 11, return to device, then unshift it << 11 in the received phase on the device.

于 2013-08-13T01:53:27.280 回答