示例#1
1
  private String getResult(String URL, HashMap optionalParameters) {
    StringBuilder sb = new StringBuilder();
    sb.append(URL);
    try {

      Iterator iterator = optionalParameters.keySet().iterator();

      int index = 0;
      while (iterator.hasNext()) {
        if (index == 0) {
          sb.append("?");
        } else {
          sb.append("&");
        }
        String key = (String) iterator.next();
        sb.append(key);
        sb.append("=");
        sb.append(URLEncoder.encode(optionalParameters.get(key).toString(), "UTF-8"));
        index++;
      }

      URI uri = new URI(String.format(sb.toString()));
      URL url = uri.toURL();

      HttpURLConnection conn = (HttpURLConnection) url.openConnection();

      conn.setRequestMethod("GET");
      conn.setRequestProperty("Accept", "application/json");
      conn.setRequestProperty("Authorization", "Bearer " + getAccessToken());
      if (conn.getResponseCode() != 200) {
        throw new RuntimeException(
            "Failed : HTTP error code : "
                + conn.getResponseCode()
                + " - "
                + conn.getResponseMessage());
      }

      BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

      String output;
      sb = new StringBuilder();
      while ((output = br.readLine()) != null) {
        sb.append(output);
      }

      conn.disconnect();

    } catch (IOException e) {

      e.printStackTrace();
      return null;
    } catch (URISyntaxException e) {
      e.printStackTrace();
      return null;
    }
    return sb.toString();
  }
示例#2
0
  @Override
  public List<Stream> findSegmentStreams(
      int id, String[] types, String resolution, String series_type) {
    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < types.length; i++) {
      if (i != 0) {
        builder.append(",");
      }
      builder.append(types[i]);
    }

    String URL =
        "https://www.strava.com/api/v3/segments/"
            + id
            + "/streams/"
            + builder.toString()
            + "?resolution="
            + resolution;

    if (series_type != null && !series_type.isEmpty()) {
      URL += "&series_type=" + series_type;
    }

    String result = getResult(URL);
    Gson gson = new Gson();
    Stream[] streamsArray = gson.fromJson(result, Stream[].class);
    List<Stream> streams = Arrays.asList(streamsArray);
    return streams;
  }
示例#3
0
  private String getResult(String URL) {
    StringBuilder sb = new StringBuilder();

    try {
      URL url = new URL(URL);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");
      conn.setRequestProperty("Accept", "application/json");
      conn.setRequestProperty("Authorization", "Bearer " + getAccessToken());

      if (conn.getResponseCode() != 200) {
        throw new RuntimeException(
            "Failed : HTTP error code : "
                + conn.getResponseCode()
                + " - "
                + conn.getResponseMessage());
      }

      BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

      String output;
      while ((output = br.readLine()) != null) {
        sb.append(output);
      }

      conn.disconnect();

    } catch (IOException e) {

      e.printStackTrace();
      return null;
    }

    return sb.toString();
  }
示例#4
0
  @Override
  public List<Stream> findSegmentStreams(int id, String[] types) {
    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < types.length; i++) {
      if (i != 0) {
        builder.append(",");
      }
      builder.append(types[i]);
    }

    String URL = "https://www.strava.com/api/v3/segments/" + id + "/streams/" + builder.toString();
    String result = getResult(URL);
    Gson gson = new Gson();
    Stream[] streamsArray = gson.fromJson(result, Stream[].class);
    List<Stream> streams = Arrays.asList(streamsArray);
    return streams;
  }
示例#5
0
  private String putResult(String URL) {
    StringBuilder sb = new StringBuilder();

    try {
      String finalUrl = "";
      if (URL.contains("?")) {
        String[] parsedUrl = URL.split("\\?");
        String params = URLEncoder.encode(parsedUrl[1], "UTF-8");
        finalUrl = parsedUrl[0] + "?" + params;
      } else {
        finalUrl = URL;
      }

      URL url = new URL(finalUrl);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();

      conn.setRequestMethod("PUT");
      conn.setRequestProperty("Accept", "application/json");
      conn.setRequestProperty("Authorization", "Bearer " + getAccessToken());
      if (conn.getResponseCode() != 200 | conn.getResponseCode() != 201) {
        throw new RuntimeException(
            "Failed : HTTP error code : "
                + conn.getResponseCode()
                + " - "
                + conn.getResponseMessage());
      }

      BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

      String output;
      sb = new StringBuilder();
      while ((output = br.readLine()) != null) {
        sb.append(output);
      }

      conn.disconnect();

    } catch (IOException e) {

      e.printStackTrace();
      return null;
    }

    return sb.toString();
  }