List of usage examples for org.apache.http.impl.client BasicResponseHandler BasicResponseHandler
BasicResponseHandler
From source file:com.ammobyte.radioreddit.api.PerformVote.java
@Override protected Void doInBackground(String... params) { final String modhash = params[0]; final String cookie = params[1]; final String id = params[2]; final String dir = params[3]; if (MusicService.DEBUG) { Log.i(RedditApi.TAG,/*w ww.ja va 2 s.c om*/ String.format("PerformVote args: modhash=%s cookie=%s id=%s dir=%s", modhash, cookie, id, dir)); } // Prepare POST with cookie and execute it try { final HttpClient httpClient = new DefaultHttpClient(); final HttpPost httpPost = new HttpPost("http://www.reddit.com/api/vote"); final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("id", id)); nameValuePairs.add(new BasicNameValuePair("dir", dir)); nameValuePairs.add(new BasicNameValuePair("uh", modhash)); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Using HttpContext, CookieStore, and friends didn't work httpPost.setHeader("Cookie", String.format("reddit_session=\"%s\"", cookie)); httpPost.setHeader("User-Agent", RedditApi.USER_AGENT); if (MusicService.DEBUG) { // Do some extra work when debugging to print the response final ResponseHandler<String> responseHandler = new BasicResponseHandler(); final String response = httpClient.execute(httpPost, responseHandler); Log.i(RedditApi.TAG, "Reddit vote response: " + response); } else { // Otherwise just assume everything works out for now // TODO: Check for error responses and inform user of the problem httpClient.execute(httpPost); } } catch (UnsupportedEncodingException e) { Log.i(RedditApi.TAG, "UnsupportedEncodingException while performing vote", e); } catch (ClientProtocolException e) { Log.i(RedditApi.TAG, "ClientProtocolException while performing vote", e); } catch (IOException e) { Log.i(RedditApi.TAG, "IOException while performing vote", e); } catch (ParseException e) { Log.i(RedditApi.TAG, "ParseException while performing vote", e); } return null; }
From source file:org.gots.weather.provider.google.GoogleWeatherTask.java
@Override protected WeatherConditionInterface doInBackground(Object... arg0) { if (force || ws == null) { try {//from w w w . j a va 2s .c o m // android.os.Debug.waitForDebugger(); /*************/ HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url.toURI()); // create a response handler ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpclient.execute(httpget, responseHandler); // Log.d(DEBUG_TAG, "response from httpclient:n "+responseBody); ByteArrayInputStream is = new ByteArrayInputStream(responseBody.getBytes()); /* Get a SAXParser from the SAXPArserFactory. */ SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); /* Get the XMLReader of the SAXParser we created. */ XMLReader xr = sp.getXMLReader(); /* Create a new ContentHandler and apply it to the XML-Reader */ GoogleWeatherHandler gwh = new GoogleWeatherHandler(); xr.setContentHandler(gwh); // InputSource is = new InputSource(url.openStream()); /* Parse the xml-data our URL-call returned. */ xr.parse(new InputSource(is)); /* Our Handler now provides the parsed weather-data to us. */ ws = gwh.getWeatherSet(); } catch (Exception e) { Log.e("WeatherManager", "WeatherQueryError", e); } force = false; } Calendar requestCalendar = Calendar.getInstance(); requestCalendar.setTime(requestedDay); if (ws == null) return new WeatherCondition(requestedDay); else if (requestCalendar.get(Calendar.DAY_OF_YEAR) == Calendar.getInstance().get(Calendar.DAY_OF_YEAR)) return ws.getWeatherCurrentCondition(); else if (requestCalendar.get(Calendar.DAY_OF_YEAR) > Calendar.getInstance().get(Calendar.DAY_OF_YEAR)) return ws.getWeatherForecastConditions().get( requestCalendar.get(Calendar.DAY_OF_YEAR) - Calendar.getInstance().get(Calendar.DAY_OF_YEAR)); return new WeatherCondition(requestedDay); }
From source file:org.andstatus.app.net.HttpConnectionOAuthApache.java
@Override public JSONTokener getRequest(HttpGet get) throws ConnectionException { JSONTokener jso = null;/*from ww w . j a v a2 s . c o m*/ String response = null; boolean ok = false; try { if (data.oauthClientKeys.areKeysPresent()) { getConsumer().sign(get); } response = mClient.execute(get, new BasicResponseHandler()); jso = new JSONTokener(response); ok = true; } catch (Exception e) { MyLog.e(this, "Exception was caught, URL='" + get.getURI().toString() + "'", e); throw new ConnectionException(e); } if (!ok) { jso = null; } return jso; }
From source file:org.datacleaner.util.http.HttpXmlUtils.java
public String getUrlContent(String url, Map<String, String> params) throws IOException { if (params == null) { params = Collections.emptyMap(); }/*w ww . ja v a2s .c o m*/ logger.info("getUrlContent({},{})", url, params); HttpPost method = new HttpPost(url); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); for (Entry<String, String> entry : params.entrySet()) { nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } method.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String response = getHttpClient().execute(method, responseHandler); return response; }
From source file:net.noday.cat.listener.ArticleSaveNotifier.java
@Override public void onApplicationEvent(ArticleSaveEvent e) { //https://github.com/b3log/b3log-symphony/blob/master/src/main/java/org/b3log/symphony/processor/ArticleProcessor.java //https://github.com/b3log/b3log-solo/blob/master/core/src/main/java/org/b3log/solo/event/rhythm/ArticleSender.java System.out.println(e.getArticle().getTitle()); try {/*from ww w.j av a 2 s .c om*/ Article a = e.getArticle(); HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(ADD_ARTICLE_URL); post.setEntity(new StringEntity(toPostString(a), "UTF-8")); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = client.execute(post, responseHandler); log.info(responseBody); } catch (UnsupportedEncodingException ex) { log.error(ex.getMessage(), ex); } catch (ClientProtocolException ex) { log.error(ex.getMessage(), ex); } catch (IOException ex) { log.error(ex.getMessage(), ex); } }
From source file:fast.servicescreen.server.RequestServlet.java
/** * This mehtod perform a GET Request /*from w ww .ja v a 2s . c o m*/ * */ public String sendHttpRequest_GET(String url) { String responseBody = ""; // create httpClient and httpGET container HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); // Create response handler ResponseHandler<String> responseHandler = new BasicResponseHandler(); try { // send the GET request responseBody = httpclient.execute(httpget, responseHandler); } catch (Exception e) { e.printStackTrace(); responseBody = "-42"; } return responseBody; }
From source file:com.rvce.rvce8thmile.driver.TTSService.java
@Override public void onStart(Intent intent, int startId) { sayHello(str);//from w w w . j a v a 2 s.c o m Log.v(TAG, "onstart_service"); gps = new GPSTracker(getApplicationContext()); super.onStart(intent, startId); final SharedPreferences prefs = getSharedPreferences(MainActivity.class.getSimpleName(), Context.MODE_PRIVATE); busno = prefs.getString("busno", "nobuses"); license = prefs.getString("license", "unlicensed"); final android.os.Handler h = new android.os.Handler(); Runnable r = new Runnable() { @Override public void run() { // create class object // check if GPS enabled if (gps.canGetLocation()) { latitude = gps.getLatitude(); longitude = gps.getLongitude(); } else { // can't get location // GPS or Network is not enabled // Ask user to enable GPS/network in settings gps.showSettingsAlert(); } Thread t = new Thread(new Runnable() { @Override public void run() { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://rotaractrvce.com/bidn/updatebus.php"); //HttpPost httpPost=new HttpPost("http://ibmhackblind.mybluemix.net/updatebus.php"); BasicResponseHandler responseHandler = new BasicResponseHandler(); List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(4); nameValuePair.add(new BasicNameValuePair("busno", busno)); nameValuePair.add(new BasicNameValuePair("license", license)); nameValuePair.add(new BasicNameValuePair("x", Double.toString(latitude))); nameValuePair.add(new BasicNameValuePair("y", Double.toString(longitude))); try { httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair)); } catch (UnsupportedEncodingException e) { // log exception e.printStackTrace(); } try { ans = httpClient.execute(httpPost, responseHandler); //Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); } } }); t.start(); try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), latitude.toString() + " : " + longitude.toString(), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), ans, Toast.LENGTH_SHORT).show(); latitude = (double) 0; longitude = (double) 0; h.postDelayed(this, 5000); } }; h.post(r); }
From source file:fast.servicescreen.server.RequestServiceImpl.java
@Override public String sendHttpRequest_GET(String url, HashMap<String, String> headers) { // create httpClient and httpGET container DefaultHttpClient httpclient_GET = new DefaultHttpClient(); httpget = new HttpGet(url); //add all headers if (headers != null) { for (Iterator<String> iterator = headers.keySet().iterator(); iterator.hasNext();) { String tmpKey = (String) iterator.next(); String tmpVal = headers.get(tmpKey); httpget.addHeader(tmpKey, tmpVal); }/*from ww w .j a v a 2s .c om*/ } // Create response handler responseHandler = new BasicResponseHandler(); try { // send the GET request responseBody = httpclient_GET.execute(httpget, responseHandler); } catch (Exception e) { e.printStackTrace(); responseBody = "-1"; } return responseBody; }
From source file:net.volcore.wtvmaster.upload.HomepageDispatcher.java
/** Public functionality */ public void dispatch(final int gameid) { workerThreadPool.submit(new Runnable() { public void run() { String str = ""; try { HttpClient httpclient = new DefaultHttpClient(); Game game = wtvMaster.gameDB.fetchGame(gameid); GameInfo gi = game.getParsedGameInfo(); String version = ""; if (gi.gameTag == 1462982736) version = "W3XP 1." + gi.gameVersion; else version = "WAR3 1." + gi.gameVersion; HttpPost httppost = new HttpPost(url); str = "{" + "\"id\":" + gameid + "," + "\"s\":" + ((gameid * 5039) % 2311) + "," + "\"status\":" + game.getHpStatus() + "," + "\"date\":" + game.getDate() + "," + "\"name\":\"" + escape(game.getName()) + "\"," + "\"players\":\"" + escape(game.getComment()) + "\"," + "\"streamer\":\"" + escape(game.getStreamer()) + "\"," + "\"length\":" + game.getGameLength() + "," + "\"version\":\"" + version + "\"," + "\"mapname\":\"" + escape(gi.mapName) + "\"," + "\"mapcheck\":" + gi.mapCheck + "," + "\"certified\": " + game.getCertified() + "," + "\"organisation\": \"" + escape(game.getOrganisation()) + "\"" + "}"; httppost.setEntity(new ByteArrayEntity(str.getBytes())); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpclient.execute(httppost, responseHandler); } catch (Exception e) { logger.error("Failed to update hp: " + e); logger.error("Request was: " + str); e.printStackTrace();//from www. j a va2 s . c om } } }); }
From source file:net.ruthandtodd.gpssync.rkoauth.RunkeeperOAuthClient.java
public String getAccessToken() throws Exception { Server server = null;/*from w ww. j a v a 2 s . c o m*/ try { synchronized (lock) { final int callbackPort = getEphemeralPort(); String callbackUri = "http://localhost:" + callbackPort + CALLBACK_PATH; if (server == null) { // Start an HTTP rkoauth: server = new Server(callbackPort); for (Connector c : server.getConnectors()) { c.setHost("localhost"); // local clients only } server.setHandler(newCallback()); server.start(); } String authorizationURL = RunkeeperService.getAuthorizationUri(callbackUri); BareBonesBrowserLaunch.browse(authorizationURL); // wait on the lock. callback to the handler on the rkoauth will clear the lock. lock.wait(); // something must have hit the lock, so we hope that 'code' has been set String content = RunkeeperService.getTokenUri(code, callbackUri); HttpClient httpclient = new DefaultHttpClient(); HttpPost post = new HttpPost(content); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpclient.execute(post, responseHandler); JsonNode rootNode = new ObjectMapper().readValue(responseBody, JsonNode.class); String accessToken = rootNode.get("access_token").asText(); return accessToken; } } catch (Exception e) { } finally { if (server != null) { try { server.stop(); } catch (Exception e) { e.printStackTrace(); } } } return ""; }