List of usage examples for org.apache.http.client ClientProtocolException toString
public String toString()
From source file:gpsalarm.app.service.PostMonitor.java
private void pollForProximityAlerts(Account myAccount2) { String baseURI = "http://" + targetDomain + ":" + targetPort + "/wsdbServiceWAR/position"; DefaultHttpClient httpClient = new DefaultHttpClient(); HttpHost targetHost = new HttpHost(targetDomain, targetPort, "http"); Serializer s = new Persister(); HttpGet httpget = null;/* www . ja v a 2 s. com*/ String urlToSendRequest = baseURI + "/get/alerts"; httpget = new HttpGet(urlToSendRequest); // Make sure the server knows what kind of a response we will accept httpget.addHeader("Accept", "application/xml"); // Also be sure to tell the server what kind of content we are sending httpget.addHeader("Content-Type", "application/xml"); try { // execute is a blocking call, it's best to call this code in a thread separate from the ui's HttpResponse response = httpClient.execute(targetHost, httpget); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity en = response.getEntity(); if (en != null) { String str = convertStreamToString(en.getContent()); Alerts aList = s.read(Alerts.class, str); List<Alert> list = aList.getProperties(); if (list == null) return; Long now = Calendar.getInstance().getTime().getTime(); boolean alertFlag = false; for (Alert a : list) { Reminder r = rdb.getReminderByGid(a.getGid().toString()); if (r != null && a.getUsername() != null) { a.setTimestamp(now); if (rdb.updateAlerts(a)) alertFlag = true; } } rdb.markDeleteAlerts(now); if (alertFlag) showProximityNotification( "there are " + rdb.getAlertsList("username").getCount() + " notifications"); } } } catch (ClientProtocolException e) { Log.e(TAG, e.toString(), e); } catch (IllegalStateException e) { Log.e(TAG, e.toString(), e); } catch (IOException e) { Log.e(TAG, e.toString(), e); } catch (Exception e) { Log.e(TAG, e.toString(), e); } finally { rdb.close(); } }
From source file:net.issarlk.androbunny.inkbunny.API.java
public boolean isLoggedIn() throws InkbunnyAPIException { //return true if there is a still valid sid if (this.sid.get() == null) { return false; }/*w w w . ja v a 2 s . c om*/ try { //Perform a simple search String tmps = AndrobunnyAppSingleton.androbunnyapp .readUri(new URI("https://inkbunny.net/api_search.php?sid=" + this.sid.get() + "&dayslimit=1&count_limit=1&no_submissions=yes")); JSONObject json_response = (JSONObject) new JSONTokener(tmps).nextValue(); json_response.get("sid"); return true; } catch (ClientProtocolException e) { throw new InkbunnyAPIException(e); } catch (URISyntaxException e) { throw new InkbunnyAPIException(e); } catch (JSONException e) { Log.e(TAG, e.toString()); return false; } catch (IOException e) { throw new InkbunnyAPIException(e); } }
From source file:org.protocoderrunner.apprunner.api.PNetwork.java
@ProtocoderScript @APIMethod(description = "Simple http get. It returns the data using the callback", example = "") @APIParam(params = { "url", "function(eventType, responseString)" }) public void httpGet(String url, final HttpGetCB callbackfn) { class RequestTask extends AsyncTask<String, String, String> { String responseString = null; @Override/*from w ww.j av a 2 s . c o m*/ protected String doInBackground(String... uri) { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response; try { URL url = new URL(uri[0]); response = httpclient.execute(new HttpGet(url.toString())); final StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpStatus.SC_OK) { ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); responseString = out.toString(); } else { // Closes the connection. response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } mHandler.post(new Runnable() { @Override public void run() { callbackfn.event(statusLine.getStatusCode(), responseString); } }); } catch (ClientProtocolException e) { MLog.e(TAG, e.toString()); } catch (IOException e) { MLog.e(TAG, e.toString()); } finally { MLog.e(TAG, "error"); } return responseString; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); // Do anything with response.. } } MLog.d(TAG, "" + new RequestTask().execute(url)); }
From source file:org.meerkat.services.WebApp.java
/** * checkWebAppStatus//from ww w .j av a 2 s. com * * @return WebAppResponse */ public WebAppResponse checkWebAppStatus() { // Set the response at this point to empty in case of no response at all setCurrentResponse(""); int statusCode = 0; // Create an instance of HttpClient. MeerkatHttpClient meerkatClient = new MeerkatHttpClient(); DefaultHttpClient httpclient = meerkatClient.getHttpClient(); WebAppResponse response = new WebAppResponse(); response.setResponseAppType(); // Create a method instance. HttpGet httpget = new HttpGet(url); // Measure the response time Counter c = new Counter(); c.startCounter(); // Execute the method. HttpResponse httpresponse = null; try { httpresponse = httpclient.execute(httpget); // Set the http status statusCode = httpresponse.getStatusLine().getStatusCode(); } catch (ClientProtocolException e) { log.error("Client Protocol Exception", e); response.setHttpStatus(0); response.setHttpTextResponse(e.toString()); setCurrentResponse(e.toString()); response.setContainsWebAppExpectedString(false); c.stopCounter(); response.setPageLoadTime(c.getDurationSeconds()); httpclient.getConnectionManager().shutdown(); return response; } catch (IOException e) { log.error("IOException - " + e.getMessage()); response.setHttpStatus(0); response.setHttpTextResponse(e.toString()); setCurrentResponse(e.toString()); response.setContainsWebAppExpectedString(false); c.stopCounter(); response.setPageLoadTime(c.getDurationSeconds()); httpclient.getConnectionManager().shutdown(); return response; } response.setHttpStatus(statusCode); // Consume the response body try { httpresponse.getEntity().getContent().toString(); } catch (IllegalStateException e) { log.error("IllegalStateException", e); } catch (IOException e) { log.error("IOException", e); } // Get the response BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(httpresponse.getEntity().getContent())); } catch (IllegalStateException e1) { log.error("IllegalStateException in http buffer", e1); } catch (IOException e1) { log.error("IOException in http buffer", e1); } String readLine; String responseBody = ""; try { while (((readLine = br.readLine()) != null)) { responseBody += "\n" + readLine; } } catch (IOException e) { log.error("IOException in http response", e); } try { br.close(); } catch (IOException e) { log.error("Closing BufferedReader", e); } response.setHttpTextResponse(responseBody); setCurrentResponse(responseBody); // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); if (statusCode != HttpStatus.SC_OK) { log.warn("Httpstatus code: " + statusCode + " | Method failed: " + httpresponse.getStatusLine()); } // Check if the response contains the expectedString if (getCurrentResponse().contains(expectedString)) { response.setContainsWebAppExpectedString(true); } // Stop the counter c.stopCounter(); response.setPageLoadTime(c.getDurationSeconds()); return response; }
From source file:com.FluksoViz.FluksoVizActivity.java
private boolean isFluksoRechableOverHTTP() { HttpClient httpclient = new DefaultHttpClient(); HttpParams httpParams = httpclient.getParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 1000); HttpConnectionParams.setSoTimeout(httpParams, 1000); StatusLine statusLine = null;/*from ww w . j a v a 2 s.c om*/ HttpResponse response = null; try { response = httpclient.execute(new HttpGet("http://" + ip_addr + ":8080/")); statusLine = response.getStatusLine(); if (statusLine != null) { if (statusLine.getStatusCode() == HttpStatus.SC_OK) { return true; } else { Toast.makeText(FluksoVizActivity.this, "bad IP?", Toast.LENGTH_LONG).show(); response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } else { throw new IOException(); } } catch (ClientProtocolException e) { e.printStackTrace(); Toast.makeText(FluksoVizActivity.this, R.string.exception + "\n" + e.toString(), Toast.LENGTH_LONG) .show(); return false; } catch (SocketTimeoutException e) { Toast.makeText(FluksoVizActivity.this, R.string.flukso_ip_address_is_wrong + "\n" + e.toString(), Toast.LENGTH_LONG).show(); e.printStackTrace(); return false; } catch (IOException e) { Toast.makeText(FluksoVizActivity.this, R.string.exception + "\n" + e.toString(), Toast.LENGTH_LONG) .show(); e.printStackTrace(); return false; } }
From source file:org.wso2.mobile.idp.proxy.utils.ServerUtilities.java
/** * * @param context// w ww . j a va2 s .co m * @param url * @param params * @param clientID * @param clientSecret * @return */ public static Map<String, String> postData(String url, Map<String, String> params, String clientID, String clientSecret) { // Create a new HttpClient and Post Header Map<String, String> responseParams = new HashMap<String, String>(); HttpClient httpclient = getCertifiedHttpClient(); Log.d(TAG, "Posting '" + params.toString() + "' to " + url); StringBuilder bodyBuilder = new StringBuilder(); Iterator<Entry<String, String>> iterator = params.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, String> param = iterator.next(); bodyBuilder.append(param.getKey()).append('=').append(param.getValue()); if (iterator.hasNext()) { bodyBuilder.append('&'); } } String body = bodyBuilder.toString(); Log.d(TAG, "Posting '" + body + "' to " + url); byte[] postData = body.getBytes(); HttpPost httppost = new HttpPost(url); httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); httppost.setHeader("Accept", "*/*"); httppost.setHeader("User-Agent", "Mozilla/5.0 ( compatible ), Android"); String authorizationString = "Basic " + new String(Base64.encodeBase64((clientID + ":" + clientSecret).getBytes())); //this line is diffe httppost.setHeader("Authorization", authorizationString); Log.e("AUTH : STRING : ", authorizationString); try { httppost.setEntity(new ByteArrayEntity(postData)); HttpResponse response = httpclient.execute(httppost); responseParams.put("response", getResponseBody(response)); responseParams.put("status", String.valueOf(response.getStatusLine().getStatusCode())); Log.d(TAG, responseParams.get("response")); return responseParams; } catch (ClientProtocolException e) { Log.d(TAG, e.toString()); return null; } catch (IOException e) { JSONObject obj = new JSONObject(); try { e.printStackTrace(); obj.put("error_description", "Internal Server Error"); obj.put("error", "Internal Server Error"); } catch (JSONException e1) { Log.d(TAG, e1.toString()); } responseParams.put("response", obj.toString()); responseParams.put("status", "500"); return responseParams; } }