From 605c09e0b5f0da4dca023176eee297844c7f415e Mon Sep 17 00:00:00 2001 From: Onur Avsar Date: Mon, 13 Feb 2012 19:55:32 +0200 Subject: [PATCH 1/3] Added Fetcher. --- src/org/janis/http/Fetcher.java | 92 +++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/org/janis/http/Fetcher.java diff --git a/src/org/janis/http/Fetcher.java b/src/org/janis/http/Fetcher.java new file mode 100644 index 0000000..717c73e --- /dev/null +++ b/src/org/janis/http/Fetcher.java @@ -0,0 +1,92 @@ +/* + * @author Onur Avsar + */ +package org.janis.http; + +import java.io.IOException; + +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; + +public class Fetcher { + + private HttpClient httpClient; + private HttpPost httpPost; + private HttpResponse httpResponse; + private String url; + + /** + * @param url the url + */ + public Fetcher(String url) { + this.url = url; + httpClient = new DefaultHttpClient(); + } + + /** + * @return the httpResponse + */ + public String fetch() throws ClientProtocolException, IOException { + httpPost = new HttpPost(url); + httpResponse = httpClient.execute(httpPost); + String result = EntityUtils.toString(httpResponse.getEntity()); + return result; + } + + /** + * @param nameValuePairs the nameValuePairs to create parameters + * @return the httpResponse + */ + public String fetch(NameValuePair[] nameValuePairs) throws ClientProtocolException, IOException { + StringBuffer encodedParameters = new StringBuffer("?"); + for (NameValuePair nameValuePair: nameValuePairs) { + encodedParameters.append(nameValuePair.getName() + "=" + nameValuePair.getValue() + "&"); + } + encodedParameters.deleteCharAt(encodedParameters.length() - 1); + url += encodedParameters; + httpPost = new HttpPost(url); + httpResponse = httpClient.execute(httpPost); + String result = EntityUtils.toString(httpResponse.getEntity()); + return result; + } + + /** + * @return the httpClient + */ + public HttpClient getHttpClient() { + return httpClient; + } + + /** + * @param httpClient the httpClient to set + */ + public void setHttpClient(HttpClient httpClient) { + this.httpClient = httpClient; + } + + /** + * @return the URL + */ + public String getUrl() { + return url; + } + + /** + * @param url the URL to set + */ + public void setUrl(String url) { + this.url = url; + } + + /** + * @return the httpResponse + */ + public HttpResponse getHttpResponse() { + return httpResponse; + } +} \ No newline at end of file From 33d966a6ea5f31718bcb8bc2d475d78705a0963a Mon Sep 17 00:00:00 2001 From: Onur Avsar Date: Mon, 13 Feb 2012 22:35:11 +0200 Subject: [PATCH 2/3] Some changes are made for consistent code styling and parameters are encoded. --- src/org/janis/http/Fetcher.java | 57 +++++++++++++++++---------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/src/org/janis/http/Fetcher.java b/src/org/janis/http/Fetcher.java index 717c73e..63a6198 100644 --- a/src/org/janis/http/Fetcher.java +++ b/src/org/janis/http/Fetcher.java @@ -4,6 +4,7 @@ package org.janis.http; import java.io.IOException; +import java.net.URLEncoder; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; @@ -14,59 +15,59 @@ import org.apache.http.util.EntityUtils; public class Fetcher { - - private HttpClient httpClient; - private HttpPost httpPost; - private HttpResponse httpResponse; + + private HttpClient mHttpClient; + private HttpPost mHttpPost; + private HttpResponse mHttpResponse; private String url; - + /** - * @param url the url + * @param url the URL */ public Fetcher(String url) { this.url = url; - httpClient = new DefaultHttpClient(); + mHttpClient = new DefaultHttpClient(); } - + /** - * @return the httpResponse + * @return the result */ public String fetch() throws ClientProtocolException, IOException { - httpPost = new HttpPost(url); - httpResponse = httpClient.execute(httpPost); - String result = EntityUtils.toString(httpResponse.getEntity()); - return result; + mHttpPost = new HttpPost(url); + mHttpResponse = mHttpClient.execute(mHttpPost); + String result = EntityUtils.toString(mHttpResponse.getEntity()); + return result; } - + /** - * @param nameValuePairs the nameValuePairs to create parameters - * @return the httpResponse + * @param nameValuePairs the mNameValuePairs to create parameters + * @return the result */ public String fetch(NameValuePair[] nameValuePairs) throws ClientProtocolException, IOException { StringBuffer encodedParameters = new StringBuffer("?"); for (NameValuePair nameValuePair: nameValuePairs) { - encodedParameters.append(nameValuePair.getName() + "=" + nameValuePair.getValue() + "&"); + encodedParameters.append(URLEncoder.encode(nameValuePair.getName()) + "=" + URLEncoder.encode(nameValuePair.getValue()) + "&"); } encodedParameters.deleteCharAt(encodedParameters.length() - 1); url += encodedParameters; - httpPost = new HttpPost(url); - httpResponse = httpClient.execute(httpPost); - String result = EntityUtils.toString(httpResponse.getEntity()); - return result; + mHttpPost = new HttpPost(url); + mHttpResponse = mHttpClient.execute(mHttpPost); + String result = EntityUtils.toString(mHttpResponse.getEntity()); + return result; } /** - * @return the httpClient + * @return the mHttpClient */ public HttpClient getHttpClient() { - return httpClient; + return mHttpClient; } /** - * @param httpClient the httpClient to set + * @param mHttpClient the mHttpClient to set */ public void setHttpClient(HttpClient httpClient) { - this.httpClient = httpClient; + this.mHttpClient = httpClient; } /** @@ -84,9 +85,9 @@ public void setUrl(String url) { } /** - * @return the httpResponse + * @return the mHttpResponse */ public HttpResponse getHttpResponse() { - return httpResponse; + return mHttpResponse; } -} \ No newline at end of file +} From 75d5bd23daccf311235d2eb6cbdd7d1e2488b20a Mon Sep 17 00:00:00 2001 From: Onur Avsar Date: Fri, 24 Feb 2012 12:45:18 +0200 Subject: [PATCH 3/3] HttpGet is used instead of HttpPost. --- src/org/janis/http/Fetcher.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/org/janis/http/Fetcher.java b/src/org/janis/http/Fetcher.java index 63a6198..353e530 100644 --- a/src/org/janis/http/Fetcher.java +++ b/src/org/janis/http/Fetcher.java @@ -10,14 +10,14 @@ import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; public class Fetcher { private HttpClient mHttpClient; - private HttpPost mHttpPost; + private HttpGet mHttpGet; private HttpResponse mHttpResponse; private String url; @@ -33,8 +33,8 @@ public Fetcher(String url) { * @return the result */ public String fetch() throws ClientProtocolException, IOException { - mHttpPost = new HttpPost(url); - mHttpResponse = mHttpClient.execute(mHttpPost); + mHttpGet = new HttpGet(url); + mHttpResponse = mHttpClient.execute(mHttpGet); String result = EntityUtils.toString(mHttpResponse.getEntity()); return result; } @@ -50,8 +50,8 @@ public String fetch(NameValuePair[] nameValuePairs) throws ClientProtocolExcepti } encodedParameters.deleteCharAt(encodedParameters.length() - 1); url += encodedParameters; - mHttpPost = new HttpPost(url); - mHttpResponse = mHttpClient.execute(mHttpPost); + mHttpGet = new HttpGet(url); + mHttpResponse = mHttpClient.execute(mHttpGet); String result = EntityUtils.toString(mHttpResponse.getEntity()); return result; }