oak.http
Class OkHttpTool

java.lang.Object
  extended by oak.http.OkHttpTool

public class OkHttpTool
extends Object

This is a utility class for abstracting commonly used OkHttp methods Created by ericrichardson on 1/6/14.


Field Summary
static String DELETE
           
static String GET
           
static String POST
           
static String PUT
           
 
Constructor Summary
OkHttpTool()
          Constructor with no Context.
OkHttpTool(android.content.Context context)
          Constructor with a Context to install a ResponseCache
 
Method Summary
 HttpURLConnection delete(String url)
          Basic DELETE Request
 HttpURLConnection get(String url)
          Basic GET Request
 BufferedReader getBufferedInputReader(HttpURLConnection connection)
           
 Map<String,String> getDefaultHeaders()
          Returns the default request headers
 HttpURLConnection post(String url, org.apache.http.entity.StringEntity entity)
          Basic POST Request
 HttpURLConnection put(String url, org.apache.http.entity.StringEntity entity)
          Basic PUT Request
 void setCertValidationDisabled(boolean isDisabled)
          Deprecated. 
 void setConnectTimeout(long timeout)
          Set's OkHttpClients Connect Timeout.
 void setDefaultHeaders(Map<String,String> defaultHeaders)
          Set default headers to set on all Http Requests
 void setPreEmptiveBasicAuth(String targetHost, String username, String password)
          Sets Basic Auth for all requests
 void setReadTimeout(long timeout)
          Set's OkHttpClients Read Timeout.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PUT

public static final String PUT
See Also:
Constant Field Values

POST

public static final String POST
See Also:
Constant Field Values

GET

public static final String GET
See Also:
Constant Field Values

DELETE

public static final String DELETE
See Also:
Constant Field Values
Constructor Detail

OkHttpTool

public OkHttpTool(android.content.Context context)
Constructor with a Context to install a ResponseCache

Parameters:
context - used to install ResponseCache

OkHttpTool

public OkHttpTool()
Constructor with no Context. No Response Cache is made, since there is no Context to get a cache directory

Method Detail

setReadTimeout

public void setReadTimeout(long timeout)
Set's OkHttpClients Read Timeout.

Parameters:
timeout - Time is Milliseconds to wait until Timeout

setConnectTimeout

public void setConnectTimeout(long timeout)
Set's OkHttpClients Connect Timeout.

Parameters:
timeout - Time is Milliseconds to wait until Timeout

setDefaultHeaders

public void setDefaultHeaders(Map<String,String> defaultHeaders)
Set default headers to set on all Http Requests

Parameters:
defaultHeaders - Map of http headers

getDefaultHeaders

public Map<String,String> getDefaultHeaders()
Returns the default request headers

Returns:
mDefaultHeaders

get

public HttpURLConnection get(String url)
                      throws IOException
Basic GET Request

Parameters:
url - URL to send GET request
Returns:
HttpUrlConnection
Throws:
IOException

post

public HttpURLConnection post(String url,
                              org.apache.http.entity.StringEntity entity)
                       throws IOException
Basic POST Request

Parameters:
url - URL to send POST request
entity - StringEntity to send as the body of the POST request
Returns:
HttpUrlConnection
Throws:
IOException

put

public HttpURLConnection put(String url,
                             org.apache.http.entity.StringEntity entity)
                      throws IOException
Basic PUT Request

Parameters:
url - URL to send PUT request
entity - StringEntity to send as the body of the PUT request
Returns:
HttpUrlConnection
Throws:
IOException

delete

public HttpURLConnection delete(String url)
                         throws IOException
Basic DELETE Request

Parameters:
url - URL to send DELETE request
Returns:
HttpUrlConnection
Throws:
IOException

setCertValidationDisabled

@Deprecated
public void setCertValidationDisabled(boolean isDisabled)
Deprecated. 

This method makes the app vulnerable to a plethora of nasty things such as man-in-the-middle. Only enable this for debugging if you know what you're doing and your backend has SSL issues. After you enable it, slap your backend people and tell them to fix SSL ASAP before you ship the app/

Please never leave this enabled.

Parameters:
isDisabled -

setPreEmptiveBasicAuth

public void setPreEmptiveBasicAuth(String targetHost,
                                   String username,
                                   String password)
Sets Basic Auth for all requests

Parameters:
targetHost - Host to authenticate to
username - Username of user
password - password as user

getBufferedInputReader

public BufferedReader getBufferedInputReader(HttpURLConnection connection)
                                      throws IOException
Parameters:
connection - HttpUrlConnection to get a reader from
Returns:
BufferedReader of HttpUrlConnections InputStream
Throws:
IOException


Copyright © 2011-2014. All Rights Reserved.