Back to project page on-the-hook.
The source code is released under:
MIT License
If you think the Android project on-the-hook listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.yoandinkov.onthehook; /*from w w w . j a v a2s. co m*/ import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import com.yoandinkov.onthehook.models.HttpResponseWrapper; import com.yoandinkov.onthehook.models.HttpType; import com.yoandinkov.onthehook.models.RequestPurpose; import com.yoandinkov.onthehook.models.Response; import android.app.Activity; import android.content.SharedPreferences; import android.os.AsyncTask; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; public abstract class RequestActivity extends Activity implements OnClickListener { /** * Session key is stored from login until logout every request except * register or login requires session key value as header. */ private String sessionKey; /** Common url for every request on server. */ private String apiUrl = "http://on-the-hook.herokuapp.com/api"; // common methods protected abstract void handleResponse(HttpResponseWrapper response); public abstract void onClick(View v); // Http Requesters private String getCredentials() { String result = ""; SharedPreferences credentials = getSharedPreferences("credentials", MODE_PRIVATE); result = credentials.getString("sessionKey", null); return result; } protected class HttpGetRequester extends AsyncTask<String, Boolean, Response> { private RequestPurpose currentPurpose; @Override protected Response doInBackground(String... params) { Response currentResponse = new Response(); HttpClient httpClient = new DefaultHttpClient(); try { String url = params[0]; HttpGet currentRequest = new HttpGet(url); currentRequest.addHeader("content-type", "application/json"); currentRequest.addHeader("x-SessionKey", RequestActivity.this.getCredentials()); HttpResponse response = httpClient.execute(currentRequest); InputStream responseInputStream = response.getEntity() .getContent(); InputStreamReader responseInputStreamReader = new InputStreamReader( responseInputStream); BufferedReader responseBufferedReader = new BufferedReader( responseInputStreamReader); String chunk = null; StringBuilder responseBody = new StringBuilder(); while ((chunk = responseBufferedReader.readLine()) != null) { responseBody.append(chunk); } currentResponse.setBody(responseBody.toString()); currentResponse.setStatusCode(response.getStatusLine() .getStatusCode()); } catch (Exception ex) { Log.e("OnTheHook", ex.getMessage()); } finally { httpClient.getConnectionManager().shutdown(); } return currentResponse; } @Override protected void onPostExecute(Response result) { super.onPostExecute(result); HttpResponseWrapper currentResponse = new HttpResponseWrapper(); currentResponse.setPurpose(this.getCurrentPurpose()); currentResponse.setType(HttpType.GET); currentResponse.setResponse(result); RequestActivity.this.handleResponse(currentResponse); } public RequestPurpose getCurrentPurpose() { return currentPurpose; } public void setCurrentPurpose(RequestPurpose currentPurpose) { this.currentPurpose = currentPurpose; } } protected class HttpPutRequester extends AsyncTask<String, Boolean, Response> { private RequestPurpose currentPurpose; @Override protected Response doInBackground(String... params) { Response currentResponse = new Response(); HttpClient httpClient = new DefaultHttpClient(); try { String url = params[0]; HttpPut currentRequest = new HttpPut(url); String requestBodyJson = params[1]; StringEntity requestBody = new StringEntity(requestBodyJson); currentRequest.addHeader("content-type", "application/json"); currentRequest.addHeader("x-SessionKey", RequestActivity.this.getCredentials()); currentRequest.setEntity(requestBody); HttpResponse response = httpClient.execute(currentRequest); InputStream responseInputStream = response.getEntity() .getContent(); InputStreamReader responseInputStreamReader = new InputStreamReader( responseInputStream); BufferedReader responseBufferedReader = new BufferedReader( responseInputStreamReader); String chunk = null; StringBuilder responseBody = new StringBuilder(); while ((chunk = responseBufferedReader.readLine()) != null) { responseBody.append(chunk); } currentResponse.setBody(responseBody.toString()); currentResponse.setStatusCode(response.getStatusLine() .getStatusCode()); } catch (Exception ex) { Log.e("OnTheHook", ex.getMessage()); } finally { httpClient.getConnectionManager().shutdown(); } return currentResponse; } @Override protected void onPostExecute(Response result) { super.onPostExecute(result); HttpResponseWrapper currentResponse = new HttpResponseWrapper(); currentResponse.setResponse(result); currentResponse.setType(HttpType.PUT); currentResponse.setPurpose(this.getCurrentPurpose()); RequestActivity.this.handleResponse(currentResponse); } public RequestPurpose getCurrentPurpose() { return currentPurpose; } public void setCurrentPurpose(RequestPurpose currentPurpose) { this.currentPurpose = currentPurpose; } } protected class HttpPostRequester extends AsyncTask<String, Boolean, Response> { private RequestPurpose currentPurpose; @Override protected Response doInBackground(String... params) { Response currentResponse = new Response(); HttpClient httpClient = new DefaultHttpClient(); try { String url = params[0]; HttpPost currentRequest = new HttpPost(url); String requestBodyJson = params[1]; StringEntity requestBody = new StringEntity(requestBodyJson); currentRequest.addHeader("content-type", "application/json"); currentRequest.addHeader("x-SessionKey", RequestActivity.this.getCredentials ()); currentRequest.setEntity(requestBody); HttpResponse response = httpClient.execute(currentRequest); InputStream responseInputStream = response.getEntity() .getContent(); InputStreamReader responseInputStreamReader = new InputStreamReader( responseInputStream); BufferedReader responseBufferedReader = new BufferedReader( responseInputStreamReader); String chunk = null; StringBuilder responseBody = new StringBuilder(); while ((chunk = responseBufferedReader.readLine()) != null) { responseBody.append(chunk); } currentResponse.setBody(responseBody.toString()); currentResponse.setStatusCode(response.getStatusLine() .getStatusCode()); } catch (Exception ex) { Log.e("OnTheHook", ex.getMessage()); } finally { httpClient.getConnectionManager().shutdown(); } return currentResponse; } @Override protected void onPostExecute(Response result) { super.onPostExecute(result); HttpResponseWrapper currentResponse = new HttpResponseWrapper(); currentResponse.setResponse(result); currentResponse.setType(HttpType.POST); currentResponse.setPurpose(this.getCurrentPurpose()); RequestActivity.this.handleResponse(currentResponse); } public RequestPurpose getCurrentPurpose() { return currentPurpose; } public void setCurrentPurpose(RequestPurpose currentPurpose) { this.currentPurpose = currentPurpose; } } // Auto-generated getters/setters protected String getApiUrl() { return apiUrl; } protected void setApiUrl(String apiUrl) { this.apiUrl = apiUrl; } protected String getSessionKey() { SharedPreferences credentials = getSharedPreferences("credentials", MODE_PRIVATE); String sessionKey = credentials.getString("sessionKey", null); return sessionKey; } protected void setSessionKey(String sessionKey) { this.sessionKey = sessionKey; } }