Created
May 9, 2012 07:31
-
-
Save Axxiss/2642692 to your computer and use it in GitHub Desktop.
Restful client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RestClient { | |
private static final String TAG = "RestClient"; | |
private static final String USER_AGENT = "VisioStorm Android"; | |
/** Methods supported by RestClient */ | |
public enum RequestMethod { | |
GET, POST | |
} | |
/** URL of the request */ | |
private URL mUrl = null; | |
/** Request mHeaders */ | |
private ArrayList<Header> mHeaders = null; | |
/** Request mParameters */ | |
private ArrayList<NameValuePair> mParameters = null; | |
/** HTTP client that will launch the request */ | |
private AndroidHttpClient mHttpClient = null; | |
/** Request to be send */ | |
private HttpUriRequest mHttpRequest = null; | |
/** Response received from the server */ | |
private HttpResponse mHttpResponse = null; | |
private UsernamePasswordCredentials mCredentials = null; | |
/** | |
* Creates a new instance of the REST client | |
* | |
* @param mUrl | |
* @throws MalformedURLException | |
*/ | |
public RestClient(String url) throws MalformedURLException { | |
mUrl = new URL(url); | |
mHeaders = new ArrayList<Header>(); | |
mParameters = new ArrayList<NameValuePair>(); | |
mHttpClient = AndroidHttpClient.newInstance(USER_AGENT); | |
} | |
/** | |
* Add a header to the request | |
* | |
* @param header | |
* Header to add | |
*/ | |
public void addHeader(Header header) { | |
mHeaders.add(header); | |
} | |
/** | |
* Execute an HTTP request | |
* | |
* @param request | |
* Request to be executed | |
*/ | |
private void executeRequest() { | |
try { | |
mHttpResponse = mHttpClient.execute(mHttpRequest); | |
} catch (ClientProtocolException e) { | |
Log.e(TAG, "Error in HTTP protocol"); | |
e.printStackTrace(); | |
mHttpRequest.abort(); | |
mHttpResponse = null; | |
} catch (IOException e) { | |
Log.e(TAG, "I/O Error"); | |
e.printStackTrace(); | |
mHttpRequest.abort(); | |
mHttpResponse = null; | |
} finally { | |
// shutdown connection to ensure deallocation of all system | |
// resources | |
mHttpClient.close(); | |
} | |
} | |
public void execute(RequestMethod method) throws NoSuchMethodException, | |
URISyntaxException { | |
execute(method, null, null); | |
} | |
/** | |
* Create a full HTTP request adding mParameters and mHeaders. And finally | |
* execute the request calling executeRequest | |
* | |
* @param method | |
* method to be execute | |
* @throws NoSuchMethodException | |
* throw when Request method does not exist | |
* @throws URISyntaxException | |
* @throws UnsupportedEncodingException | |
*/ | |
public void execute(RequestMethod method, String user, String pass) | |
throws NoSuchMethodException, URISyntaxException { | |
mHttpRequest = null; | |
switch (method) { | |
// URL?param1=value&...¶mN=value | |
case GET: { | |
String params = ""; | |
if (!mParameters.isEmpty()) { | |
params = "?"; | |
int i; | |
try { | |
for (i = 0; i < mParameters.size() - 1; i++) { | |
params += mParameters.get(i).getName() | |
+ "=" | |
+ URLEncoder.encode(mParameters.get(i) | |
.getValue(), "UTF-8") + "&"; | |
} | |
params += mParameters.get(i).getName() | |
+ "=" | |
+ URLEncoder.encode(mParameters.get(i).getValue(), | |
"UTF-8"); | |
} catch (UnsupportedEncodingException e) { | |
Log.e(TAG, "Unsupported encoding, check if UTF-8 is used"); | |
e.printStackTrace(); | |
} | |
} | |
mHttpRequest = new HttpGet(mUrl.toURI() + params); | |
} | |
break; | |
case POST: { | |
mHttpRequest = new HttpPost(mUrl.toURI()); | |
if (!mParameters.isEmpty()) { | |
UrlEncodedFormEntity encodedParams = null; | |
try { | |
encodedParams = new UrlEncodedFormEntity(mParameters, | |
HTTP.UTF_8); | |
} catch (UnsupportedEncodingException e) { | |
Log.e(TAG, "POST unsupported encoding"); | |
e.printStackTrace(); | |
return; | |
} | |
((HttpPost) mHttpRequest).setEntity(encodedParams); | |
} | |
} | |
break; | |
default: | |
throw new NoSuchMethodException(); | |
} | |
if (user != null && pass != null) { | |
mCredentials = new UsernamePasswordCredentials(user, pass); | |
try { | |
Header header = new BasicScheme().authenticate(mCredentials, | |
mHttpRequest); | |
mHeaders.add(header); | |
} catch (AuthenticationException e) { | |
Log.e(TAG, "Authentication header error"); | |
e.printStackTrace(); | |
} | |
} | |
for (Header header : mHeaders) { | |
mHttpRequest.addHeader(header); | |
} | |
executeRequest(); | |
} | |
public void addParam(String param, String value) { | |
mParameters | |
.add(new BasicNameValuePair(param, URLEncoder.encode(value))); | |
} | |
public HttpResponse getResponse() { | |
return this.mHttpResponse; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment