Skip to content

Instantly share code, notes, and snippets.

@akshaydashrath
Last active March 25, 2024 13:54

Revisions

  1. akshaydashrath revised this gist Apr 8, 2014. 1 changed file with 13 additions and 2 deletions.
    15 changes: 13 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,15 @@
    void prepareRequest(HttpURLConnection connection, Request request)
    @Override
    public Response execute(Request request) throws IOException {
    HttpsURLConnection connection = (HttpsURLConnection) super.openConnection(request);
    try {
    prepareRequest(connection, request);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return readResponse(connection);
    }

    void prepareRequest(HttpURLConnection connection, Request request)
    throws IOException, OAuthExpectationFailedException, NoSuchAlgorithmException,
    OAuthCommunicationException, OAuthMessageSignerException {
    if (Build.VERSION.SDK_INT > 13) {
    @@ -26,4 +37,4 @@ void prepareRequest(HttpURLConnection connection, Request request)
    if (body!=null) {
    body.writeTo( connection.getOutputStream() );
    }
    }
    }
  2. akshaydashrath created this gist Apr 8, 2014.
    29 changes: 29 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    void prepareRequest(HttpURLConnection connection, Request request)
    throws IOException, OAuthExpectationFailedException, NoSuchAlgorithmException,
    OAuthCommunicationException, OAuthMessageSignerException {
    if (Build.VERSION.SDK_INT > 13) {
    connection.setRequestProperty("Connection", "close");
    }
    connection.setRequestMethod(request.getMethod());
    connection.setDoInput(true);
    for (Header header : request.getHeaders()) {
    connection.addRequestProperty(header.getName(), header.getValue());
    }
    TypedOutput body = request.getBody();
    long length = -1;
    if (body != null) {
    connection.setDoOutput(true);
    length = body.length();
    }
    RequestSigner.create(CONSUMER_KEY, CONSUMER_SECRET)
    .setOAuthTokenAndSecret(getToken(request), getoAuthSecret(request))
    .sign(request, connection);
    if (length != -1) {
    connection.setFixedLengthStreamingMode((int) length);
    } else {
    connection.setChunkedStreamingMode(CHUNK_SIZE);
    }
    if (body!=null) {
    body.writeTo( connection.getOutputStream() );
    }
    }