Пример #1
0
  @Test
  public void testHttpSession() {
    On.req(
        new ReqHandler() {
          @Override
          public Object execute(Req req) throws Exception {
            Log.info("Session", "ID", req.sessionId(), "data", req.session());

            int n = req.session("n", 0) + 1;
            req.session().put("n", n);

            int m = req.session("m", 10) + 1;
            req.session().put("m", m);

            return n + ":" + m;
          }
        });

    HttpClient client = HTTP.keepCookies(true).dontClose();

    eq(client.get(localhost("/a")).fetch(), "1:11");
    eq(client.get(localhost("/b")).fetch(), "2:12");
    eq(client.get(localhost("/c")).fetch(), "3:13");

    client.close();

    client = HTTP.dontClose().keepCookies(true); // do it again

    eq(client.get(localhost("/a")).fetch(), "1:11");
    eq(client.get(localhost("/b")).fetch(), "2:12");
    eq(client.get(localhost("/c")).fetch(), "3:13");

    client.close();
  }
Пример #2
0
  public static void getWeather(String cityName) {
    Log.d(LOG_TAG, "Getting weather info for: " + cityName);
    http.get(
        APIURL + QUERY + cityName + AMP + UNITS + units + AMP + APIKEY,
        new AsyncResponse() {
          @Override
          public void callback(JSONObject res) {
            Log.d(LOG_TAG, "Weather callback");
            if (res != null) {
              try {
                JSONObject main = res.getJSONObject(MAIN);
                tempCur = main.getString(MAIN_TEMP);
                tempMin = main.getString(MAIN_TMIN);
                tempMax = main.getString(MAIN_TMAX);
                humidity = main.getString(MAIN_HUMD);
                pressure = main.getString(MAIN_PRES);

                if (units.equals("imperial")) {
                  tempUnit = "°F";
                } else if (units.equals("metric")) {
                  tempUnit = "°C";
                } else {
                  tempUnit = "K";
                }

                JSONArray w = res.getJSONArray(WEATHER);
                for (int i = 0; i < w.length(); i++) {
                  JSONObject wo = w.getJSONObject(i);
                  weather = wo.getString(WEATHER_MAIN);
                  description = wo.getString(WEATHER_DESC);
                  icon = wo.getString(WEATHER_ICON);
                }

                name = res.getString(NAME);
              } catch (JSONException e) {
                e.printStackTrace();
              }

              Intent msg = new Intent("weather");
              msg.putExtra("name", name);
              msg.putExtra("weather", weather);
              msg.putExtra("description", description);
              msg.putExtra("tempCur", tempCur);
              msg.putExtra("tempMin", tempMin);
              msg.putExtra("tempMax", tempMax);
              msg.putExtra("humidity", humidity);
              msg.putExtra("pressure", pressure);
              msg.putExtra("icon", icon);
              msg.putExtra("tempUnit", tempUnit);
              context.sendBroadcast(msg);
            }
          }
        });
  }
Пример #3
0
 /**
  * Returns a stream of random sample of all public statuses. The default access level provides a
  * small proportion of the Firehose. The "Gardenhose" access level provides a proportion more
  * suitable for data mining and research applications that desire a larger proportion to be
  * statistically significant sample.
  *
  * @return StatusStream
  * @throws TwitterException when Twitter service or network is unavailable
  * @see twitter4j.StatusStream
  * @see <a href="https://dev.twitter.com/docs/streaming-api/methods">Streaming API: Methods
  *     statuses/sample</a>
  * @since Twitter4J 2.0.10
  */
 StatusStream getSampleStream() throws TwitterException {
   ensureAuthorizationEnabled();
   try {
     return new StatusStreamImpl(
         getDispatcher(),
         http.get(
             conf.getStreamBaseURL() + "statuses/sample.json?" + stallWarningsGetParam,
             null,
             auth,
             null),
         conf);
   } catch (IOException e) {
     throw new TwitterException(e);
   }
 }
Пример #4
0
 void requestToken(String authorizationCode) {
   try {
     String url = this.service_.getTokenEndpoint();
     StringBuilder ss = new StringBuilder();
     ss.append("grant_type=authorization_code")
         .append("&client_id=")
         .append(Utils.urlEncode(this.service_.getClientId()))
         .append("&client_secret=")
         .append(Utils.urlEncode(this.service_.getClientSecret()))
         .append("&redirect_uri=")
         .append(Utils.urlEncode(this.service_.getGenerateRedirectEndpoint()))
         .append("&code=")
         .append(authorizationCode);
     HttpClient client = new HttpClient(this);
     client.setTimeout(15);
     client
         .done()
         .addListener(
             this,
             new Signal2.Listener<Exception, HttpMessage>() {
               public void trigger(Exception event1, HttpMessage event2) {
                 OAuthProcess.this.handleToken(event1, event2);
               }
             });
     Method m = this.service_.getTokenRequestMethod();
     if (m == Method.Get) {
       boolean hasQuery = url.indexOf('?') != -1;
       url += (hasQuery ? '&' : '?') + ss.toString();
       client.get(url);
     } else {
       HttpMessage post = new HttpMessage();
       post.setHeader("Content-Type", "application/x-www-form-urlencoded");
       post.addBodyText(ss.toString());
       client.post(url, post);
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }