Android Open Source - android-tools Rest A P I






From Project

Back to project page android-tools.

License

The source code is released under:

MIT License

If you think the Android project android-tools listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.comfreeze.lib.api;
//from ww  w  .  j a  va  2s.co  m
import android.os.Bundle;
import android.util.Log;

import net.comfreeze.lib.api.helper.JSONHelper;

import org.apache.http.HttpResponse;
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.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.json.custom.JSONException;
import org.json.custom.JSONObject;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.Iterator;

abstract public class RestAPI extends BaseAPI {
    private static final String TAG = RestAPI.class.getSimpleName();

    public static String readErrors(JSONObject error) {
        if (!silent)
            Log.i(TAG, "Parsing errors");
        StringBuilder builder = new StringBuilder();
        Object errors = JSONHelper.get(error, RESPONSE_ERROR_MESSAGE);
        if (null == errors)
            return null;
        if (errors instanceof String) {
            builder.append(errors);
        } else if (errors instanceof JSONObject) {
            JSONObject errorObj = (JSONObject) errors;
            @SuppressWarnings("unchecked")
            Iterator<String> keys = errorObj.keys();
            while (keys.hasNext())
                builder.append((builder.length() > 0 ? "\n" : "") + JSONHelper.getString(errorObj, keys.next()));
        }
        return builder.toString();
    }

    public JSONObject list(Bundle parameters) {
        return list(getEndpoint(), parameters);
    }

    public JSONObject list(String endpoint, Bundle parameters) {
        if (!silent)
            Log.d(TAG, "GET: " + endpoint);
        HttpResponse response = null;
        JSONObject jsonResponse = null;
        int responseCode = defaultResponseCode;
        String responseMessage = defaultResponseMessage;
        HttpGet request = (HttpGet) getRequest(Type.GET, endpoint);
        if (null != parameters && parameters.size() > 0)
            request.setURI(URI.create(request.getURI().toString() + (request.getURI().toString().contains("?") ? "&" : "?") + buildQueryString(parameters)));
        if (!silent)
            Log.d(TAG, "REQUEST: " + request.getURI().toString());
        response = execute(request);
        String responseString = buildResponseString(response);
        if (!silent)
            Log.d(TAG, "RAW RESPONSE: " + responseString);
        jsonResponse = buildJsonObject(responseString);
        if (null != response && null != response.getStatusLine()) {
            responseCode = response.getStatusLine().getStatusCode();
            responseMessage = response.getStatusLine().getReasonPhrase();
        }
        if (!silent)
            Log.d(TAG, "RESPONSE: " + jsonResponse);
        if (null == jsonResponse) {
            jsonResponse = new JSONObject();
        }
        try {
            jsonResponse.put(RESPONSE_STATUS, responseCode);
            jsonResponse.put(RESPONSE_MESSAGE, responseMessage);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonResponse;
    }

    public JSONObject create(Bundle parameters) {
        return create(getEndpoint(), parameters);
    }

    public JSONObject create(String endpoint, Bundle parameters) {
        if (!silent)
            Log.d(TAG, "POST: " + endpoint);
        HttpResponse response = null;
        JSONObject jsonResponse = null;
        int responseCode = defaultResponseCode;
        String responseMessage = defaultResponseMessage;
        HttpPost request = (HttpPost) getRequest(Type.POST, endpoint);
        JSONObject jsonRequest = buildJsonObject(parameters);
        if (!silent)
            Log.d(TAG, "REQUEST: " + jsonRequest);
        try {
            request.setEntity(new StringEntity(jsonRequest.toString()));
            response = execute(request);
            String responseString = buildResponseString(response);
            if (!silent)
                Log.d(TAG, "RAW RESPONSE: " + responseString);
            jsonResponse = buildJsonObject(responseString);
            if (null != response && null != response.getStatusLine()) {
                responseCode = response.getStatusLine().getStatusCode();
                responseMessage = response.getStatusLine().getReasonPhrase();
            }
            if (!silent)
                Log.d(TAG, "RESPONSE: " + jsonResponse);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (null == jsonResponse) {
            jsonResponse = new JSONObject();
        }
        try {
            jsonResponse.put(RESPONSE_STATUS, responseCode);
            jsonResponse.put(RESPONSE_MESSAGE, responseMessage);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonResponse;
    }

    public JSONObject upload(Bundle parameters, File file) {
        return upload(getEndpoint(), parameters, file);
    }

    public JSONObject upload(String endpoint, Bundle parameters, File file) {
        if (!silent)
            Log.d(TAG, "POST: " + endpoint);
        HttpResponse response = null;
        JSONObject jsonResponse = null;
        int responseCode = defaultResponseCode;
        String responseMessage = defaultResponseMessage;
        HttpPost request = (HttpPost) getRequest(Type.POST, endpoint, null);
        JSONObject jsonRequest = buildJsonObject(parameters);
        if (!silent)
            Log.d(TAG, "REQUEST: " + jsonRequest);
        try {
            MultipartEntity entity = new MultipartEntity();// HttpMultipartMode.BROWSER_COMPATIBLE);
            entity.addPart("body", new StringBody(jsonRequest.toString()));
            entity.addPart("photo[photo_bn]", new FileBody(file));
            // StringEntity entity = new StringEntity(jsonRequest.toString());
            request.setEntity(entity);
            // request.setHeader(HTTP.CONTENT_TYPE, "multipart/form-data");
            response = execute(request);
            String responseString = buildResponseString(response);
            if (!silent)
                Log.d(TAG, "RAW RESPONSE: " + responseString);
            jsonResponse = buildJsonObject(responseString);
            if (null != response && null != response.getStatusLine()) {
                responseCode = response.getStatusLine().getStatusCode();
                responseMessage = response.getStatusLine().getReasonPhrase();
            }
            if (!silent)
                Log.d(TAG, "RESPONSE: " + jsonResponse);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (null == jsonResponse) {
            jsonResponse = new JSONObject();
        }
        try {
            jsonResponse.put(RESPONSE_STATUS, responseCode);
            jsonResponse.put(RESPONSE_MESSAGE, responseMessage);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonResponse;
    }

    public JSONObject update(Bundle parameters) {
        return update(getEndpoint(), parameters);
    }

    public JSONObject update(String endpoint, Bundle parameters) {
        if (!silent)
            Log.d(TAG, "PUT: " + endpoint);
        HttpResponse response = null;
        JSONObject jsonResponse = null;
        int responseCode = defaultResponseCode;
        String responseMessage = defaultResponseMessage;
        HttpPut request = (HttpPut) getRequest(Type.PUT, endpoint);
        JSONObject jsonRequest = buildJsonObject(parameters);
        if (!silent)
            Log.d(TAG, "REQUEST: " + jsonRequest);
        try {
            request.setEntity(new StringEntity(jsonRequest.toString()));
            response = execute(request);
            String responseString = buildResponseString(response);
            if (!silent)
                Log.d(TAG, "RAW RESPONSE: " + responseString);
            jsonResponse = buildJsonObject(responseString);
            if (null != response && null != response.getStatusLine()) {
                responseCode = response.getStatusLine().getStatusCode();
                responseMessage = response.getStatusLine().getReasonPhrase();
            }
            if (!silent)
                Log.d(TAG, "RESPONSE: " + jsonResponse);
        } catch (UnsupportedEncodingException e) {
            if (!silent)
                Log.e(TAG, "Exception encountered", e);
            e.printStackTrace();
        }
        if (null == jsonResponse) {
            jsonResponse = new JSONObject();
        }
        try {
            jsonResponse.put(RESPONSE_STATUS, responseCode);
            jsonResponse.put(RESPONSE_MESSAGE, responseMessage);
        } catch (JSONException e) {
            if (!silent)
                Log.e(TAG, "Exception encountered", e);
            e.printStackTrace();
        }
        return jsonResponse;
    }

    public JSONObject delete(Bundle parameters) {
        return delete(getEndpoint(), parameters);
    }

    public JSONObject delete(String endpoint, Bundle parameters) {
        if (!silent)
            Log.d(TAG, "GET: " + endpoint);
        HttpResponse response = null;
        JSONObject jsonResponse = null;
        int responseCode = defaultResponseCode;
        String responseMessage = defaultResponseMessage;
        HttpDelete request = (HttpDelete) getRequest(Type.DELETE, endpoint);
        if (null != parameters && parameters.size() > 0)
            request.setURI(URI.create(request.getURI().toString() + (request.getURI().toString().contains("?") ? "&" : "?") + buildQueryString(parameters)));
        if (!silent)
            Log.d(TAG, "REQUEST: " + request.getURI().toString());
        response = execute(request);
        String responseString = buildResponseString(response);
        if (!silent)
            Log.d(TAG, "RAW RESPONSE: " + responseString);
        jsonResponse = buildJsonObject(responseString);
        if (null != response && null != response.getStatusLine()) {
            responseCode = response.getStatusLine().getStatusCode();
            responseMessage = response.getStatusLine().getReasonPhrase();
        }
        if (!silent)
            Log.d(TAG, "RESPONSE: " + jsonResponse);
        if (null == jsonResponse) {
            jsonResponse = new JSONObject();
        }
        try {
            jsonResponse.put(RESPONSE_STATUS, responseCode);
            jsonResponse.put(RESPONSE_MESSAGE, responseMessage);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonResponse;
    }
}




Java Source Code List

net.comfreeze.lib.BundleBuilder.java
net.comfreeze.lib.CFZApplication.java
net.comfreeze.lib.ContentValueBuilder.java
net.comfreeze.lib.FragmentMap.java
net.comfreeze.lib.adapter.HeaderListAdapter.java
net.comfreeze.lib.adapter.IHeaderListAdapter.java
net.comfreeze.lib.adapter.SeparatedListAdapter.java
net.comfreeze.lib.api.BaseAPI.java
net.comfreeze.lib.api.RestAPI.java
net.comfreeze.lib.api.XMLAPI.java
net.comfreeze.lib.api.helper.CursorHelper.java
net.comfreeze.lib.api.helper.JSONHelper.java
net.comfreeze.lib.api.helper.ModelHelper.java
net.comfreeze.lib.api.xml.WordpressAPI.java
net.comfreeze.lib.audio.SoundManager.java
net.comfreeze.lib.db.DatabaseHelper.java
net.comfreeze.lib.db.DatabaseTable.java
net.comfreeze.lib.db.helper.HelperCursor.java
net.comfreeze.lib.db.model.CFZModel.java
net.comfreeze.lib.db.model.FieldColumnMap.java
net.comfreeze.lib.fragments.CFZListFragment.java
net.comfreeze.lib.provider.CFZSimpleProvider.java
net.comfreeze.lib.service.CFZService.java
net.comfreeze.lib.ui.SupportFragmentActivity.java
net.comfreeze.lib.ui.dialog.CFZDialogProgress.java
net.comfreeze.lib.ui.fragment.CFZFragmentBase.java
net.comfreeze.lib.views.BiScrollView.java
net.comfreeze.lib.views.CFZViewHelper.java
net.comfreeze.lib.views.FlowLayout.java
net.comfreeze.lib.views.GestureHelper.java
net.comfreeze.lib.views.HeaderListView.java
net.comfreeze.lib.views.ResizingView.java
net.comfreeze.lib.views.ViewCollection.java
net.comfreeze.lib.views.ViewUtils.java
net.comfreeze.lib.xml.XMLParser.java
net.comfreeze.lib.xml.wordpress.FeedXmlParser.java