Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android使用RestService(带/不带缓存)_Android - Fatal编程技术网

Android使用RestService(带/不带缓存)

Android使用RestService(带/不带缓存),android,Android,我目前正在创建一个高性能的移动应用程序。现在,我正在研究使用rest服务的各种设计模式。其中一个突出的模式就是谷歌IO讨论。我是如何看待开发这个设计的代码的。我将使用SpringREST使用序列化库对POJO执行实际的HTTP Rest和序列化。我遇到了这个实现,并将使用它作为我的应用程序的蓝图。现在有一个主要问题 public interface HttpMethods { public Object getForObject(Object ... params); publi

我目前正在创建一个高性能的移动应用程序。现在,我正在研究使用rest服务的各种设计模式。其中一个突出的模式就是谷歌IO讨论。我是如何看待开发这个设计的代码的。我将使用SpringREST使用序列化库对POJO执行实际的HTTP Rest和序列化。我遇到了这个实现,并将使用它作为我的应用程序的蓝图。现在有一个主要问题

public interface HttpMethods {
    public Object getForObject(Object ... params);
    public Object putForObject(Object ... params);
}



   public class LocationsHttpMethods implements HttpMethods{

    private final Context mContext;

    public LocationsHttpMethods(Context context)
    {
        mContext=context;
    }
    @Override
    public Location[] getForObject(Object... params) {
        return null;
    }

    @Override
    public Object putForObject(Object... params) {
        return null;
    }

}
我的位置只是一个pojo类。现在困扰我的问题是,我给出的第二个链接只是使用布尔值返回数据。我将返回一个数组

package com.confiz.rest.services;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.confiz.rest.providers.IProvider;
import com.confiz.rest.providers.LocationsProvider;

public class ProcessorService extends Service
{
    private Integer lastStartId;
    private final Context mContext = this;

    /**
     * The keys to be used for the required actions to start this service.
     */
    public static class Extras
    {
        /**
         * The provider which the called method is on.
         */
        public static final String PROVIDER_EXTRA = "PROVIDER_EXTRA";

        /**
         * The method to call.
         */
        public static final String METHOD_EXTRA = "METHOD_EXTRA";

        /**
         * The action to used for the result intent.
         */
        public static final String RESULT_ACTION_EXTRA = "RESULT_ACTION_EXTRA";

        /**
         * The extra used in the result intent to return the result.
         */
        public static final String RESULT_EXTRA = "RESULT_EXTRA";
    }

    private final HashMap<String, AsyncServiceTask> mTasks = new HashMap<String, AsyncServiceTask>();
    /**
     * Identifier for each supported provider.
     * Cannot use 0 as Bundle.getInt(key) returns 0 when the key does not exist.
     */
    public static class Providers
    {
        public static final int LOATIONS_PROVIDER = 1;
    }

    private IProvider GetProvider(int providerId)
    {
        switch(providerId)
        {
        case Providers.LOATIONS_PROVIDER:
            return new LocationsProvider(this);
        }
        return null;
    }

    /**
     * Builds a string identifier for this method call.
     * The identifier will contain data about:
     *   What processor was the method called on
     *   What method was called
     *   What parameters were passed
     * This should be enough data to identify a task to detect if a similar task is already running.
     */
    private String getTaskIdentifier(Bundle extras)
    {
        String[] keys = extras.keySet().toArray(new String[0]);
        java.util.Arrays.sort(keys);
        StringBuilder identifier = new StringBuilder();

        for (int keyIndex = 0; keyIndex < keys.length; keyIndex++)
        {
            String key = keys[keyIndex];

            // The result action may be different for each call.
            if (key.equals(Extras.RESULT_ACTION_EXTRA))
            {
                continue;
            }

            identifier.append("{");
            identifier.append(key);
            identifier.append(":");
            identifier.append(extras.get(key).toString());
            identifier.append("}");
        }

        return identifier.toString();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        // This must be synchronised so that service is not stopped while a new task is being added.
        synchronized (mTasks)
        {
            // stopSelf will be called later and if a new task is being added we do not want to stop the service.
            lastStartId = startId;

            Bundle extras = intent.getExtras();

            String taskIdentifier = getTaskIdentifier(extras);

            Log.i("ProcessorService", "starting " + taskIdentifier);

            // If a similar task is already running then lets use that task.
            AsyncServiceTask task = mTasks.get(taskIdentifier);

            if (task == null)
            {
                task = new AsyncServiceTask(taskIdentifier, extras);

                mTasks.put(taskIdentifier, task);

                // AsyncTasks are by default only run in serial (depending on the android version)
                // see android documentation for AsyncTask.execute()
                task.execute((Void[]) null);
            }

            // Add this Result Action to the task so that the calling activity can be notified when the task is complete.
            String resultAction = extras.getString(Extras.RESULT_ACTION_EXTRA);
            if (resultAction != "")
            {
                task.addResultAction(extras.getString(Extras.RESULT_ACTION_EXTRA));
            }
        }

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    public class AsyncServiceTask extends AsyncTask<Void, Void, Object>
    {
        private final Bundle mExtras;
        private final ArrayList<String> mResultActions = new ArrayList<String>();
        private final String mTaskIdentifier;

        /**
         * Constructor for AsyncServiceTask
         * 
         * @param taskIdentifier A string which describes the method being called.
         * @param extras         The Extras from the Intent which was used to start this method call.
         */
        public AsyncServiceTask(String taskIdentifier, Bundle extras)
        {
            mTaskIdentifier = taskIdentifier;
            mExtras = extras;
        }

        public void addResultAction(String resultAction)
        {
            if (!mResultActions.contains(resultAction))
            {
                mResultActions.add(resultAction);
            }
        }

        @Override
        protected Object doInBackground(Void... params)
        {
            Log.i("ProcessorService", "working " + mTaskIdentifier);
            Object result = false;
            final int providerId = mExtras.getInt(Extras.PROVIDER_EXTRA);
            final int methodId = mExtras.getInt(Extras.METHOD_EXTRA);

            if (providerId != 0 && methodId != 0)
            {
                final IProvider provider = GetProvider(providerId);

                if (provider != null)
                {
                    try
                    {
                        result = provider.RunTask(methodId, mExtras);
                    } catch (Exception e)
                    {
                        result = false;
                    }
                }

            }

            return result;
        }

        @Override
        protected void onPostExecute(Object result)
        {
            // This must be synchronised so that service is not stopped while a new task is being added.
            synchronized (mTasks)
            {
                Log.i("ProcessorService", "finishing " + mTaskIdentifier);
                // Notify the caller(s) that the method has finished executing
                for (int i = 0; i < mResultActions.size(); i++)
                {
                    Intent resultIntent = new Intent(mResultActions.get(i));

                    //What to do here
                    resultIntent.put(Extras.RESULT_EXTRA, true);
                    //What to do here ends.

                    resultIntent.putExtras(mExtras);
                    resultIntent.setPackage(mContext.getPackageName());
                    mContext.sendBroadcast(resultIntent);
                }

                // The task is complete so remove it from the running tasks list
                mTasks.remove(mTaskIdentifier);

                // If there are no other executing methods then stop the service
                if (mTasks.size() < 1)
                {
                    stopSelf(lastStartId);
                }
            }
        }
    }
}
现在,如果您浏览到包含AsyncService的代码,并将resultIntent.putExtras.RESULT_EXTRA设置为true

现在,我应该如何将数据传递回intent。我听说Serializable不好,而packable是丑陋的代码。我还能用什么。其次,我应该在哪里添加SQL缓存检索代码。如何将此代码添加到框架中。希望我有道理