Exemplo n.º 1
0
  public void testSetAndGetLastfmUserInfo() throws Exception {
    final String userInfoJSON =
        "\n"
            + "{\"user\":{\"name\":\"RJ\",\"realname\":\"Richard Jones \",\"image\":[{\"#text\":\"http:\\/\\/userserve-ak.last.fm\\/serve\\/34\\/84504153.jpg\",\"size\":\"small\"},{\"#text\":\"http:\\/\\/userserve-ak.last.fm\\/serve\\/64\\/84504153.jpg\",\"size\":\"medium\"},{\"#text\":\"http:\\/\\/userserve-ak.last.fm\\/serve\\/126\\/84504153.jpg\",\"size\":\"large\"},{\"#text\":\"http:\\/\\/userserve-ak.last.fm\\/serve\\/252\\/84504153.jpg\",\"size\":\"extralarge\"}],\"url\":\"http:\\/\\/www.last.fm\\/user\\/RJ\",\"id\":\"1000002\",\"country\":\"UK\",\"age\":\"31\",\"gender\":\"m\",\"subscriber\":\"1\",\"playcount\":\"89564\",\"playlists\":\"4\",\"bootstrap\":\"0\",\"registered\":{\"#text\":\"2002-11-20 11:50\",\"unixtime\":\"1037793040\"},\"type\":\"alumni\"}}";

    assertNull(WAILSettings.getLastfmUserInfo(getContext()));
    WAILSettings.setLastfmUserInfo(getContext(), userInfoJSON);
    assertEquals(
        LFUserResponseModel.parseFromJSON(userInfoJSON),
        WAILSettings.getLastfmUserInfo(getContext()));
  }
  public static LFUserResponseModel parseFromJSON(String json) throws LFApiException {
    try {
      final LFUserResponseModel userModel = new LFUserResponseModel(json);
      final JSONObject userJson = new JSONObject(json).getJSONObject("user");

      userModel.id = userJson.optString("id");
      userModel.name = userJson.optString("name");
      userModel.realName = userJson.optString("realname");
      userModel.url = userJson.optString("url");
      userModel.country = userJson.optString("country");
      userModel.age = userJson.optString("age");
      userModel.gender = userJson.optString("gender");
      userModel.subscriber = userJson.optInt("subscriber");
      userModel.playCount = userJson.optInt("playcount");
      userModel.playlists = userJson.optInt("playlists");
      userModel.registered = Registered.fromJSON(userJson.optJSONObject("registered"));

      return userModel;
    } catch (JSONException e) {
      throw LFApiException.newDataFormatErrorInstance(null, e.getMessage());
    }
  }