SweetzpotAS / StravaZpot-Android

A fluent API to integrate with Strava on Android apps
70 stars 35 forks source link

Activity - Temperature #2

Closed IanMSpencer closed 7 years ago

IanMSpencer commented 7 years ago

When downloading activities generated via a Garmin, i.e. fully populated, the Temperature field of Activity causes an error in JSON - OBJECT expected, number received.

I tried fully populating the Temperature class which seems to be a stub using Distance as a model, but the error still occurs.

Then spotted the type adapters - Temperature missing.

Fixes:

Temperature.java

public class Temperature { private float celsiusDegrees; public static Temperature celsiusDegrees(float celsiusDegrees) { return new Temperature(celsiusDegrees); } public Temperature(float celsiusDegrees) { this.celsiusDegrees = celsiusDegrees; }

public float getCelsiusDegrees() {
    return celsiusDegrees;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Temperature temperature = (Temperature) o;

    return Float.compare(temperature.celsiusDegrees, celsiusDegrees) == 0;

}

@Override
public int hashCode() {
    return (celsiusDegrees != +0.0f ? Float.floatToIntBits(celsiusDegrees) : 0);
}

@Override
public String toString() {
    return String.valueOf(celsiusDegrees);
}

}

TemperatureTypeAdapter

public class TemperatureTypeAdapter extends TypeAdapter {

@Override
public void write(JsonWriter out, Temperature value) throws IOException {
    out.value(value.getCelsiusDegrees());
}

@Override
public Temperature read(JsonReader in) throws IOException {
    if(!in.peek().equals(JsonToken.NULL)) {
        return new Temperature((float) (in.nextDouble()));
    } else {
        return null;
    }
}

}

and register the typeadapter in config.java

truizlop commented 7 years ago

Thanks for your suggestion Ian. Since it seems you already got the fix for this issue, could you submit a pull request? If not, could you provide the JSON that is causing the issue, so that we can reproduce it and fix it ourselves? Thanks!

IanMSpencer commented 7 years ago

Hi

Haven't yet worked out how to properly use GitHub, had to use a workaround to rebuild the code for my app under Android Studio 2.2 as I don't really understand the gradle system yet.

The problem will only occur if the data includes the average temperature of the activity. To reproduce: 1) Connect to an account where activities have been created using a device that records temperature (e.g. Garmin 800 series) and download an appropriate ride. 2)I think if you add average_temp to the data created in your activity test it will fail when you round trip the data.

The code is simply incomplete, so not a big deal to sort out once you look!

// Get rides from strava AthleteAPI athleteAPI = new AthleteAPI(config); Athlete athlete = athleteAPI.retrieveCurrentAthlete().execute(); ActivityAPI activityAPI = new ActivityAPI(config); int page = 1; List activity_list = null; do { Log.i(TAG, "doing page " + page);

// ** THIS FAILS WITH REAL DATA FROM GARMIN activity_list = activityAPI.listMyActivities().inPage(page) .execute(); // *** ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ DecimalFormat df = new DecimalFormat("#.#"); df.setRoundingMode(RoundingMode.HALF_UP);

    for (int i = 0; i < activity_list.size(); i++) {
        ride_array_list.add(activity_list.get(i).getName() + ": " + df.format(activity_list.get(i).getDistance().getMeters() / 1000));
       activity_list.get(i).getID(); }
        page++;
   } while (activity_list.size() > 0);
    return ride_array_list;`

Not being unhelpful - just don't know what I'm doing!

truizlop commented 7 years ago

Don't worry, I'll try to provide a fix soon. You have provided valuable feedback to discover the issue ;)

truizlop commented 7 years ago

Hi Ian, I have just fixed the issue and published version 1.0.1 of our library. Please, update your Gradle build file to check out the new version where this issue is solved.