コード例 #1
0
ファイル: GarminUploader.java プロジェクト: hholzgra/runnerup
  @Override
  public Status listWorkouts(List<Pair<String, String>> list) {
    Status s;
    if ((s = connect()) != Status.OK) {
      return s;
    }

    HttpURLConnection conn = null;
    Exception ex = null;
    try {
      conn = (HttpURLConnection) new URL(LIST_WORKOUTS_URL).openConnection();
      conn.setRequestMethod("GET");
      addCookies(conn);
      conn.connect();
      getCookies(conn);
      InputStream in = new BufferedInputStream(conn.getInputStream());
      JSONObject obj = parse(in);
      conn.disconnect();
      int responseCode = conn.getResponseCode();
      String amsg = conn.getResponseMessage();
      if (responseCode == 200) {
        obj = obj.getJSONObject("com.garmin.connect.workout.dto.BaseUserWorkoutListDto");
        JSONArray arr = obj.getJSONArray("baseUserWorkouts");
        for (int i = 0; ; i++) {
          obj = arr.optJSONObject(i);
          if (obj == null) break;
          list.add(
              new Pair<String, String>(
                  obj.getString("workoutId"), obj.getString("workoutName") + ".json"));
        }
        return Status.OK;
      }
      ex = new Exception(amsg);
    } catch (IOException e) {
      ex = e;
    } catch (JSONException e) {
      ex = e;
    }

    s = Uploader.Status.ERROR;
    s.ex = ex;
    if (ex != null) {
      ex.printStackTrace();
    }
    return s;
  }
コード例 #2
0
ファイル: GarminUploader.java プロジェクト: hholzgra/runnerup
  @Override
  public Status upload(SQLiteDatabase db, long mID) {
    Status s;
    if ((s = connect()) != Status.OK) {
      return s;
    }

    TCX tcx = new TCX(db);
    HttpURLConnection conn = null;
    Exception ex = null;
    try {
      StringWriter writer = new StringWriter();
      tcx.export(mID, writer);
      conn = (HttpURLConnection) new URL(UPLOAD_URL).openConnection();
      conn.setDoOutput(true);
      conn.setRequestMethod("POST");
      addCookies(conn);
      Part<StringWritable> part1 =
          new Part<StringWritable>(
              "responseContentType", new StringWritable(FormCrawler.URLEncode("text/html")));
      Part<StringWritable> part2 =
          new Part<StringWritable>("data", new StringWritable(writer.toString()));
      part2.filename = "RunnerUp.tcx";
      part2.contentType = "application/octet-stream";
      Part<?> parts[] = {part1, part2};
      postMulti(conn, parts);
      int responseCode = conn.getResponseCode();
      String amsg = conn.getResponseMessage();
      if (responseCode == 200) {
        conn.disconnect();
        return Status.OK;
      }
      ex = new Exception(amsg);
    } catch (IOException e) {
      ex = e;
    }

    s = Uploader.Status.ERROR;
    s.ex = ex;
    if (ex != null) {
      ex.printStackTrace();
    }
    return s;
  }
コード例 #3
0
ファイル: GarminUploader.java プロジェクト: hholzgra/runnerup
  @Override
  public Status connect() {
    Status s = Status.NEED_AUTH;
    s.authMethod = Uploader.AuthMethod.USER_PASS;
    if (username == null || password == null) {
      return s;
    }

    if (isConnected) {
      return Status.OK;
    }

    Exception ex = null;
    HttpURLConnection conn = null;
    logout();

    try {

      /** connect to START_URL to get cookies */
      conn = (HttpURLConnection) new URL(START_URL).openConnection();
      {
        int responseCode = conn.getResponseCode();
        String amsg = conn.getResponseMessage();
        getCookies(conn);
        if (responseCode != 200) {
          System.err.println("GarminUploader::connect() - got " + responseCode + ", msg: " + amsg);
        }
      }
      conn.disconnect();

      /** Then login using a post */
      String login = LOGIN_URL;
      FormValues kv = new FormValues();
      kv.put("login", "login");
      kv.put("login:loginUsernameField", username);
      kv.put("login:password", password);
      kv.put("login:signInButton", "Sign In");
      kv.put("javax.faces.ViewState", "j_id1");

      conn = (HttpURLConnection) new URL(login).openConnection();
      conn.setDoOutput(true);
      conn.setRequestMethod("POST");
      conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      addCookies(conn);

      {
        OutputStream wr = new BufferedOutputStream(conn.getOutputStream());
        kv.write(wr);
        wr.flush();
        wr.close();
        int responseCode = conn.getResponseCode();
        String amsg = conn.getResponseMessage();
        System.err.println("code: " + responseCode + ", msg=" + amsg);
        getCookies(conn);
      }
      conn.disconnect();

      /** An finally check that all is OK */
      conn = (HttpURLConnection) new URL(CHECK_URL).openConnection();
      addCookies(conn);
      {
        conn.connect();
        getCookies(conn);
        InputStream in = new BufferedInputStream(conn.getInputStream());
        JSONObject obj = parse(in);
        conn.disconnect();
        int responseCode = conn.getResponseCode();
        String amsg = conn.getResponseMessage();
        // Returns username(which is actually Displayname from profile) if logged in
        if (obj.optString("username", "").length() > 0) {
          isConnected = true;
          return Uploader.Status.OK;
        } else {
          System.err.println(
              "GarminUploader::connect() missing username, obj: "
                  + obj.toString()
                  + ", code: "
                  + responseCode
                  + ", msg: "
                  + amsg);
        }
        return s;
      }
    } catch (MalformedURLException e) {
      ex = e;
    } catch (IOException e) {
      ex = e;
    } catch (JSONException e) {
      ex = e;
    }

    if (conn != null) conn.disconnect();

    s = Uploader.Status.ERROR;
    s.ex = ex;
    if (ex != null) {
      ex.printStackTrace();
    }
    return s;
  }