List of usage examples for org.apache.http.client ClientProtocolException printStackTrace
public void printStackTrace()
From source file:com.batontouch.facebook.SessionStore.java
public static boolean save(Facebook session, Context context) { mcontext = context;//from www. j a v a 2 s .com Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit(); editor.putString(TOKEN, session.getAccessToken()); editor.putLong(EXPIRES, session.getAccessExpires()); facebooktoken = session.getAccessToken(); t = new Thread() { public void run() { try { authenticate(facebooktoken); } catch (ClientProtocolException e) { Log.e("my", e.getClass().getName() + e.getMessage() + "cl"); } catch (IOException e) { Log.e("my", e.getClass().getName() + e.getMessage() + "io"); e.printStackTrace(); } catch (Exception e) { Log.e("my", e.getClass().getName() + e.getMessage() + "io"); } } }; t.start(); return editor.commit(); }
From source file:org.vuphone.wwatch.asterisk.AsteriskConnector.java
/** * This is a helper method for playRecordingToPSTNNumber that will * create a new SIP extension to dial the telephone number provided. * /*from ww w . ja v a2 s . c o m*/ * @param number - a String representation of a 10 digit telephone * number that will be dialed from the newly created extension. * * @return - a String representation of the extension number that * will dial the provided telephone number, or an empty String "" * if there is an error creating the extension. */ private static String createSipExtension(String number) { String extension = ""; // The first post will open the screen to create a new generic // SIP extension HttpClient c = new DefaultHttpClient(); HttpPost post = new HttpPost("http://" + SERVER + "/admin/config.php?type=setup&display=extensions"); String authorization = Base64.base64Encode(MAINT_USERNAME + ":" + MAINT_PASSWORD); post.addHeader("Authorization", "Basic " + authorization); post.addHeader("Content-Type", "application/x-www-form-urlencoded"); String info = "display=extensions&type=setup&tech_hardware=sip_generic&Submit=Submit"; ByteArrayEntity str = new ByteArrayEntity(info.getBytes()); post.setEntity(str); try { c.execute(post); } catch (ClientProtocolException e) { e.printStackTrace(); return extension; } catch (IOException e) { e.printStackTrace(); return extension; } // The second post will create the new extension and set it to dial // the provided number. It will be named Extension<number>, with // password <number>-password. HttpClient c1 = new DefaultHttpClient(); post = new HttpPost("http://" + SERVER + "/admin/config.php"); post.addHeader("Authorization", "Basic " + authorization); post.addHeader("Content-Type", "application/x-www-form-urlencoded"); String extenNum = number; String extenName = "Extension" + extenNum; String extenSecret = extenNum + "-password"; info = "display=extensions&type=setup&action=add&extdisplay=&action=add&extdisplay=&extension=" + extenNum + "&name=" + extenName + "&cid_masquerade=&sipname=&directdid=&didalert=&mohclass=default&outboundcid=&ringtimer=0&callwaiting=enabled&emergency_cid=&tech=sip&hardware=generic&devinfo_secret=" + extenSecret + "&devinfo_dtmfmode=rfc2833&devinfo_canreinvite=no&devinfo_context=from-internal&devinfo_host=dynamic&devinfo_type=friend&devinfo_nat=yes&devinfo_port=5060&devinfo_qualify=yes&devinfo_callgroup=&devinfo_pickupgroup=&devinfo_disallow=&devinfo_allow=&devinfo_dial=" + number + "&devinfo_accountcode=&devinfo_mailbox=&faxexten=default&faxemail=&answer=0&wait=0&privacyman=0&in_default_page_grp=0&langcode=&record_in=Adhoc&record_out=Adhoc&vm=disabled&vmpwd=&email=&pager=&attach=attach%3Dno&saycid=saycid%3Dno&envelope=envelope%3Dno&delete=delete%3Dno&options=&vmcontext=default&vmx_state=&Submit=Submit"; str = new ByteArrayEntity(info.getBytes()); post.setEntity(str); try { c1.execute(post); } catch (ClientProtocolException e) { e.printStackTrace(); return extension; } catch (IOException e) { e.printStackTrace(); return extension; } // The third post will submit the changes. HttpClient c2 = new DefaultHttpClient(); post = new HttpPost("http://" + SERVER + "/admin/config.php"); post.addHeader("Authorization", "Basic " + authorization); post.addHeader("Content-Type", "application/x-www-form-urlencoded"); info = "handler=reload"; str = new ByteArrayEntity(info.getBytes()); post.setEntity(str); try { c2.execute(post); } catch (ClientProtocolException e) { e.printStackTrace(); return extension; } catch (IOException e) { e.printStackTrace(); return extension; } extension = "SIP/" + extenNum; System.out.println("I'm going to return: " + extension); return extension; }
From source file:es.ucm.look.data.remote.restful.RestMethod.java
/** * Retrieve a resource from the server/*from ww w. j a v a 2 s . c o m*/ * * @param url * Element URI to get * @return * The Element as JSON */ public static JSONObject doGet(String url) { JSONObject json = null; HttpClient httpclient = new DefaultHttpClient(); // Prepare a request object HttpGet httpget = new HttpGet(url); // Accept JSON httpget.addHeader("accept", "application/json"); // Execute the request HttpResponse response; try { response = httpclient.execute(httpget); // Get the response entity HttpEntity entity = response.getEntity(); // If response entity is not null if (entity != null) { // get entity contents and convert it to string InputStream instream = entity.getContent(); String result = convertStreamToString(instream); // construct a JSON object with result json = new JSONObject(result); // Closing the input stream will trigger connection release instream.close(); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Return the json return json; }
From source file:org.zaizi.oauth2utils.OAuthUtils.java
public static void getProtectedResource(Properties config) { String resourceURL = config.getProperty(OAuthConstants.RESOURCE_SERVER_URL); OAuth2Details oauthDetails = createOAuthDetails(config); HttpGet get = new HttpGet(resourceURL); get.addHeader(OAuthConstants.AUTHORIZATION, getAuthorizationHeaderForAccessToken(oauthDetails.getAccessToken())); DefaultHttpClient client = new DefaultHttpClient(); HttpResponse response = null;/*from w w w .j a v a 2 s .co m*/ int code = -1; try { response = client.execute(get); code = response.getStatusLine().getStatusCode(); if (code >= 400) { // Access token is invalid or expired.Regenerate the access // token System.out.println("Access token is invalid or expired. Regenerating access token...."); String accessToken = getAccessToken(oauthDetails); if (isValid(accessToken)) { // update the access token // System.out.println("New access token: " + accessToken); oauthDetails.setAccessToken(accessToken); get.removeHeaders(OAuthConstants.AUTHORIZATION); get.addHeader(OAuthConstants.AUTHORIZATION, getAuthorizationHeaderForAccessToken(oauthDetails.getAccessToken())); get.releaseConnection(); response = client.execute(get); code = response.getStatusLine().getStatusCode(); if (code >= 400) { throw new RuntimeException( "Could not access protected resource. Server returned http code: " + code); } } else { throw new RuntimeException("Could not regenerate access token"); } } handleResponse(response); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { get.releaseConnection(); } }
From source file:com.vanda.beivandalibnetwork.utils.HttpUtils.java
/** * ??/*w w w .j ava 2 s . com*/ * * @param url * ? * @param keyVals * ? * @return * @throws IOException * @throws ClientProtocolException */ public static String post(String url, Object... keyVals) { try { return execute(urlEncodedForm(new HttpPost(url), keyVals), null); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:com.pti.mates.ServerUtilities.java
private static void updatePreferences(JSONObject user, Context ctx, String fbid) { DefaultHttpClient client = new MyHttpClient(ctx); HttpPost post = new HttpPost("https://54.194.14.115:443/upload"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("user", user.toString())); try {//w w w . j ava 2s . c o m post.setHeader("Content-type", "application/json"); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } HttpResponse postResponse; try { postResponse = client.execute(post); HttpEntity responseEntity = postResponse.getEntity(); InputStream is = responseEntity.getContent(); } catch (ClientProtocolException e) { Log.e("ERROR", e.toString()); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); Log.e("ERROR", e.toString()); s = "ERROR: " + e.toString() + " :("; } Log.d("CHIVATO", "FIN THREAD"); }
From source file:com.thed.zapi.cloud.sample.FetchExecuteUpdate.java
private static Map<String, String> getExecutionsByCycleId(String uriStr, ZFJCloudRestClient client, String accessKey) throws URISyntaxException, JSONException { Map<String, String> executionIds = new HashMap<String, String>(); URI uri = new URI(uriStr); int expirationInSec = 360; JwtGenerator jwtGenerator = client.getJwtGenerator(); String jwt = jwtGenerator.generateJWT("GET", uri, expirationInSec); // System.out.println(uri.toString()); // System.out.println(jwt); HttpResponse response = null;// w w w. j a v a 2 s. co m HttpClient restClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(uri); httpGet.setHeader("Authorization", jwt); httpGet.setHeader("zapiAccessKey", accessKey); try { response = restClient.execute(httpGet); } catch (ClientProtocolException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } int statusCode = response.getStatusLine().getStatusCode(); // System.out.println(statusCode); if (statusCode >= 200 && statusCode < 300) { HttpEntity entity1 = response.getEntity(); String string1 = null; try { string1 = EntityUtils.toString(entity1); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // System.out.println(string1); JSONObject allIssues = new JSONObject(string1); JSONArray IssuesArray = allIssues.getJSONArray("searchObjectList"); // System.out.println(IssuesArray.length()); if (IssuesArray.length() == 0) { return executionIds; } for (int j = 0; j <= IssuesArray.length() - 1; j++) { JSONObject jobj = IssuesArray.getJSONObject(j); JSONObject jobj2 = jobj.getJSONObject("execution"); String executionId = jobj2.getString("id"); long IssueId = jobj2.getLong("issueId"); executionIds.put(executionId, String.valueOf(IssueId)); } } return executionIds; }
From source file:com.pti.mates.ServerUtilities.java
private static void post2(String endpoint, final String msg, final String id, final Context ctx) { DefaultHttpClient client = new MyHttpClient(ctx); HttpPost post = new HttpPost("https://54.194.14.115:443/send"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("receiver", id)); nameValuePairs.add(new BasicNameValuePair("sender", Common.getFBID())); nameValuePairs.add(new BasicNameValuePair("data", msg)); try {/*from w ww . ja v a2 s . c om*/ post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // Execute the Post call and obtain the response HttpResponse postResponse; s = "s No inicialitzada"; try { postResponse = client.execute(post); HttpEntity responseEntity = postResponse.getEntity(); InputStream is = responseEntity.getContent(); s = convertStreamToString(is); } catch (ClientProtocolException e) { Log.e("ERROR", e.toString()); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); Log.e("ERROR", e.toString()); s = "ERROR: " + e.toString() + " :("; } Log.d("CHIVATO", "FIN THREAD"); //return s; }
From source file:org.zaizi.oauth2utils.OAuthUtils.java
public static String getAccessToken(OAuth2Details oauthDetails) { HttpPost post = new HttpPost(oauthDetails.getAuthenticationServerUrl()); String clientId = oauthDetails.getClientId(); String clientSecret = oauthDetails.getClientSecret(); String scope = oauthDetails.getScope(); List<BasicNameValuePair> parametersBody = new ArrayList<BasicNameValuePair>(); parametersBody.add(new BasicNameValuePair(OAuthConstants.GRANT_TYPE, oauthDetails.getGrantType())); parametersBody.add(new BasicNameValuePair(OAuthConstants.USERNAME, oauthDetails.getUsername())); parametersBody.add(new BasicNameValuePair(OAuthConstants.PASSWORD, oauthDetails.getPassword())); if (isValid(clientId)) { parametersBody.add(new BasicNameValuePair(OAuthConstants.CLIENT_ID, clientId)); }/*from ww w . ja va2s. c o m*/ if (isValid(clientSecret)) { parametersBody.add(new BasicNameValuePair(OAuthConstants.CLIENT_SECRET, clientSecret)); } if (isValid(scope)) { parametersBody.add(new BasicNameValuePair(OAuthConstants.SCOPE, scope)); } DefaultHttpClient client = new DefaultHttpClient(); HttpResponse response = null; String accessToken = null; try { post.setEntity(new UrlEncodedFormEntity(parametersBody, HTTP.UTF_8)); response = client.execute(post); int code = response.getStatusLine().getStatusCode(); if (code >= 400) { System.out.println("Authorization server expects Basic authentication"); // Add Basic Authorization header post.addHeader(OAuthConstants.AUTHORIZATION, getBasicAuthorizationHeader(oauthDetails.getUsername(), oauthDetails.getPassword())); System.out.println("Retry with login credentials"); post.releaseConnection(); response = client.execute(post); code = response.getStatusLine().getStatusCode(); if (code >= 400) { System.out.println("Retry with client credentials"); post.removeHeaders(OAuthConstants.AUTHORIZATION); post.addHeader(OAuthConstants.AUTHORIZATION, getBasicAuthorizationHeader( oauthDetails.getClientId(), oauthDetails.getClientSecret())); post.releaseConnection(); response = client.execute(post); code = response.getStatusLine().getStatusCode(); if (code >= 400) { throw new RuntimeException( "Could not retrieve access token for user: " + oauthDetails.getUsername()); } } } Map<String, String> map = handleResponse(response); accessToken = map.get(OAuthConstants.ACCESS_TOKEN); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return accessToken; }
From source file:Main.java
public static final String postData(String url, HashMap<String, String> params) { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); String outputString = null;/*from w ww . ja v a 2 s. co m*/ try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size()); Iterator it = params.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); nameValuePairs.add(new BasicNameValuePair((String) pair.getKey(), (String) pair.getValue())); System.out.println(pair.getKey() + " = " + pair.getValue()); it.remove(); // avoids a ConcurrentModificationException } httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpURLConnection.HTTP_OK) { byte[] result = EntityUtils.toByteArray(response.getEntity()); outputString = new String(result, "UTF-8"); } } catch (ClientProtocolException e) { Log.i(CLASS_NAME, "Client protocolException happened: " + e.getMessage()); } catch (IOException e) { Log.i(CLASS_NAME, "Client IOException happened: " + e.getMessage()); } catch (NetworkOnMainThreadException e) { Log.i(CLASS_NAME, "Client NetworkOnMainThreadException happened: " + e.getMessage()); e.printStackTrace(); } catch (Exception e) { Log.i(CLASS_NAME, "Unknown exeception: " + e.getMessage()); } return outputString; }