コード例 #1
0
ファイル: HttpClientUtil.java プロジェクト: trieu/mc2ads
  public static String executePost(String url, Map<String, String> params, String accessTokens) {
    try {
      HttpClient httpClient = theThreadSafeHttpClient();
      HttpPost postRequest = new HttpPost(url);
      postRequest.addHeader("Accept-Charset", HTTP.UTF_8);
      postRequest.addHeader("User-Agent", MOBILE_USER_AGENT);
      postRequest.setHeader("Authorization", "OAuth oauth_token=" + accessTokens);

      Set<String> names = params.keySet();
      List<NameValuePair> postParameters = new ArrayList<NameValuePair>(names.size());
      for (String name : names) {
        System.out.println(name + "=" + params.get(name));
        postParameters.add(new BasicNameValuePair(name, params.get(name)));
      }
      postRequest.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8"));

      HttpResponse response = httpClient.execute(postRequest);
      HttpEntity entity = response.getEntity();
      if (entity != null) {
        httpClient.getConnectionManager().closeExpiredConnections();
        return EntityUtils.toString(entity, HTTP.UTF_8);
      }
    } catch (HttpResponseException e) {
      System.err.println(e.getMessage());

    } catch (Exception e) {
      e.printStackTrace();
    }
    return "";
  }
コード例 #2
0
  /**
   * Searching places
   *
   * @param latitude - latitude of place
   * @params longitude - longitude of place
   * @param radius - radius of searchable area
   * @param types - type of place to search
   * @return list of places
   */
  public PlacesList search(double latitude, double longitude, double radius, String types)
      throws Exception {

    this._latitude = latitude;
    this._longitude = longitude;
    this._radius = radius;

    try {

      HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
      HttpRequest request = httpRequestFactory.buildGetRequest(new GenericUrl(PLACES_SEARCH_URL));
      request.getUrl().put("key", API_KEY);
      request.getUrl().put("location", _latitude + "," + _longitude);
      request.getUrl().put("radius", _radius); // in meters
      request.getUrl().put("sensor", "false");
      if (types != null) request.getUrl().put("types", types);

      PlacesList list = request.execute().parseAs(PlacesList.class);
      // Check log cat for places response status
      Log.d("Places Status", "" + list.status);
      return list;

    } catch (HttpResponseException e) {
      Log.e("Error:", e.getMessage());
      return null;
    }
  }
コード例 #3
0
ファイル: HttpClientUtil.java プロジェクト: trieu/mc2ads
  public static final String executePost(String url) {
    try {
      HttpPost httppost = new HttpPost(url);

      httppost.setHeader("User-Agent", USER_AGENT);
      httppost.setHeader("Accept-Charset", "utf-8");
      httppost.setHeader("Cache-Control", "max-age=3, must-revalidate, private");
      httppost.setHeader(
          "Authorization",
          "OAuth oauth_token=2d62f7b3de642cdd402f62e42fba0b25, oauth_consumer_key=a324957217164fd1d76b4b60d037abec, oauth_version=1.0, oauth_signature_method=HMAC-SHA1, oauth_timestamp=1322049404, oauth_nonce=-5195915877644743836, oauth_signature=wggOr1ia7juVbG%2FZ2ydImmiC%2Ft4%3D");

      HttpClient client = theThreadSafeHttpClient();
      HttpResponse response = client.execute(httppost);
      HttpEntity entity = response.getEntity();
      if (entity != null) {
        client.getConnectionManager().closeExpiredConnections();
        return EntityUtils.toString(entity, HTTP.UTF_8);
      }

    } catch (HttpResponseException e) {
      System.err.println(e.getMessage());

    } catch (Exception e) {
      e.printStackTrace();
    }
    return "";
  }
コード例 #4
0
  /**
   * Searching single place full details
   *
   * @param reference - reference id of place - which you will get in search api request
   */
  public PlaceDetails getPlaceDetails(String reference) throws Exception {
    try {

      HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
      HttpRequest request = httpRequestFactory.buildGetRequest(new GenericUrl(PLACES_DETAILS_URL));
      request.getUrl().put("key", API_KEY);
      request.getUrl().put("reference", reference);
      request.getUrl().put("sensor", "false");

      PlaceDetails place = request.execute().parseAs(PlaceDetails.class);

      return place;

    } catch (HttpResponseException e) {
      Log.e("Error in Perform Details", e.getMessage());
      throw e;
    }
  }