Skip to content

Instantly share code, notes, and snippets.

@aftabsikander
Created October 8, 2015 12:53
Show Gist options
  • Select an option

  • Save aftabsikander/808d38397a17ac945e47 to your computer and use it in GitHub Desktop.

Select an option

Save aftabsikander/808d38397a17ac945e47 to your computer and use it in GitHub Desktop.
How to oauth url request using SignPost Android
public static WebRequestTimeout sendGetRequestToServer(Context mContext, URL para_URL)
{
HttpURLConnection getRequest = null;
WebRequestTimeout webRequestTimeout = new WebRequestTimeout();
try
{
getRequest = (HttpURLConnection) para_URL.openConnection();
String authtoken = PrefUtils.getAuthToken(mContext);
DefaultOAuthConsumer consumer = new DefaultOAuthConsumer(WebUtils.CONSUMER_KEY, WebUtils.CONSUMER_SECERT);
// sign the request (consumer is a Signpost DefaultOAuthConsumer)
consumer.setTokenWithSecret(authtoken, null);
getRequest.setConnectTimeout(CONNECT_TIMEOUT_MILLIS); // 1 minute
getRequest.addRequestProperty("Accept", "application/json");
getRequest.addRequestProperty("Content-Type", "application/json");
getRequest.setReadTimeout(READ_TIMEOUT_MILLIS);
consumer.sign(getRequest);
getRequest.getResponseCode();
// Send post request
getRequest.connect();
webRequestTimeout.setRequest(getRequest);
return webRequestTimeout;
} catch (SocketTimeoutException e)
{
e.printStackTrace();
webRequestTimeout.setIsThisTimeOutRequest(true);
return webRequestTimeout;
} catch (ConnectTimeoutException e)
{
webRequestTimeout.setIsThisConnectionTimeoutRequest(true);
e.printStackTrace();
return webRequestTimeout;
} catch (Exception e)
{
e.getMessage();
return null;
} finally
{
// clean up
/*
* try { getRequest.disconnect(); return getRequest; } catch (Exception e) { // TODO Auto-generated catch
* block e.printStackTrace(); }
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment