com.dv.sharer.android.rest.RestHttpOperations.java Source code

Java tutorial

Introduction

Here is the source code for com.dv.sharer.android.rest.RestHttpOperations.java

Source

package com.dv.sharer.android.rest;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import android.os.AsyncTask;
import android.util.Log;

import com.dv.common.json.BaseModel;
import com.dv.common.json.ITransferable;
import com.dv.common.webservices.rest.core.client.SimpleRestClient;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 *
 * @author pp76575
 */
public abstract class RestHttpOperations<T extends BaseModel & ITransferable> extends SimpleRestClient<T> {

    private static final String TAG = RestHttpOperations.class.getSimpleName();
    private final String USER_AGENT = "Mozilla/5.0";
    private final HttpClient httpclient = new DefaultHttpClient();

    public RestHttpOperations(String baseUrl, Class<T> transferableClass) {
        super(baseUrl, transferableClass);
    }

    public RestHttpOperations(String baseUrl, Class<T> transferableClass, boolean logMessages) {
        super(baseUrl, transferableClass, logMessages);
    }

    protected String getUrlAndPath() {
        return getBaseURL() + "/" + getPath() + "/";
    }

    protected HttpGet getGET(String urlStr) throws IOException {
        HttpGet httpGet = new HttpGet(urlStr);
        httpGet.setHeader("Content-Type", "application/json");
        httpGet.addHeader("Accept", "application/json");
        return httpGet;
    }

    protected HttpPost getPOST(String urlStr, String data) throws IOException {
        HttpPost httppost = new HttpPost(urlStr);
        httppost.setHeader("Content-Type", "application/json");
        StringEntity entity = new StringEntity(data);
        entity.setContentType("application/json");
        httppost.addHeader("Accept", "application/json");
        httppost.setEntity(entity);
        return httppost;
    }

    protected HttpDelete getDELETE(String urlStr) throws IOException {
        HttpDelete httpDelete = new HttpDelete(urlStr);
        httpDelete.setHeader("Content-Type", "application/json");
        httpDelete.addHeader("Accept", "application/json");
        return httpDelete;
    }

    protected String getResponse(HttpResponse r) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(r.getEntity().getContent()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return response.toString();
    }

    public HttpClient getHttpclient() {
        return httpclient;
    }

    protected class NetworkOperationPostAsyncTask extends AsyncTask<HttpPost, Void, String> {
        protected String doInBackground(HttpPost... post) {
            HttpPost http = post[0];
            try {
                HttpResponse httpResponse = getHttpclient().execute(http);
                return getResponse(httpResponse);
            } catch (Exception e) {
                Log.d(TAG, "Problem with post http operation", e);
                return null;
            }
        }
    }

    protected class NetworkOperationDeletePostAsyncTask extends AsyncTask<HttpDelete, Void, String> {
        protected String doInBackground(HttpDelete... delete) {
            HttpDelete http = delete[0];
            try {
                HttpResponse httpResponse = getHttpclient().execute(http);
                return getResponse(httpResponse);
            } catch (Exception e) {
                Log.d(TAG, "Problem with delete post http operation", e);
                return null;
            }
        }

    }

    protected class NetworkOperationGetPostAsyncTask extends AsyncTask<HttpGet, Void, String> {
        protected String doInBackground(HttpGet... get) {
            HttpGet http = get[0];
            try {
                HttpResponse httpResponse = getHttpclient().execute(http);
                return getResponse(httpResponse);
            } catch (Exception e) {
                Log.d(TAG, "Problem with get http operation", e);
                return null;
            }
        }
    }

}