Android准备等待机制或更正同步服务

时间:2016-02-17 09:27:15

标签: android

在我的Android项目中,我尝试准备等待机制或正确同步到intent service以处理多并发请求用户。例如,在单击按钮后,我的应用程序尝试使用Service

在服务器上获取数据

点击按钮后,用户可能会再次尝试点击该按钮,以管理此操作,我在下面的示例编码处理多并发请求用户,例如等待创建请求路径完成请求。

但在此代码中我知道如何创建请求堆栈,这意味着它们必须按订单的请求运行。

现在我的代码是否正确?

Volley Simple Singleton Class:

public class CustomVolleyRequestQueue {

    private static CustomVolleyRequestQueue mInstance;
    private static Context                  context;
    private        RequestQueue             mRequestQueue;

    private CustomVolleyRequestQueue(Context context) {
        this.context = context;
        mRequestQueue = getRequestQueue();
    }

    public static synchronized CustomVolleyRequestQueue getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new CustomVolleyRequestQueue(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
            Network network = new BasicNetwork(new HurlStack());
            mRequestQueue = new RequestQueue(cache, network);
            // start the volley request queue
            mRequestQueue.start();
        }
        return mRequestQueue;
    }
}

意向服务:

public class WebService extends IntentService {
    public static final String REQUEST_TAG = "SimpleWebServiceBlockingRequest";
    private RequestQueue mQueue;
    private Object       isReceiveMessagesLock;

    public WebService() {
        super("WebService");
    }

    public WebService(String name) {
        super("WebService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            Bundle data = intent.getExtras();
            if (data != null) {
                String url = data.getString("url");
                synchronized (isReceiveMessagesLock) {
                    if (isReceiveMessagesLock != null) {
                        return;
                    } else {
                        try {
                            isReceiveMessagesLock.wait();
                            startParsingTask(url); //receiving start
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }
    }

    public void startParsingTask(final String url) {
        Thread prepaire_request_thread = new Thread() {
            public void run() {
                ThreadB    request_from_servcer_thread = new ThreadB(getApplicationContext(), url);
                JSONObject json_object                 = null;
                try {
                    json_object = request_from_servcer_thread.execute().get(10, TimeUnit.SECONDS);
                } catch (InterruptedException | ExecutionException | TimeoutException e) {
                    e.printStackTrace();
                }
                if (json_object != null) {
                    Log.e("OUTPUT: ", json_object.toString());
                }
            }
        };
        prepaire_request_thread.start();
    }

    private class ThreadB extends AsyncTask<Void, Void, JSONObject> {
        private Context context;
        private String fetch_url = "";

        public ThreadB(Context ctx, String url) {
            context = ctx;
            this.fetch_url = url;
        }

        @Override
        protected JSONObject doInBackground(Void... params) {
            final RequestFuture<JSONObject> futureRequest = RequestFuture.newFuture();
            mQueue = CustomVolleyRequestQueue.getInstance(context.getApplicationContext())
                    .getRequestQueue();
            final JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method
                    .GET, fetch_url,
                    new JSONObject(), futureRequest, futureRequest);
            jsonRequest.setTag(REQUEST_TAG);
            mQueue.add(jsonRequest);
            try {
                return futureRequest.get(10, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}

POST UPDATED:

@Override
protected void onHandleIntent(Intent intent) {
    if (intent != null) {
        Bundle data = intent.getExtras();
        if (data != null) {
            String url = data.getString("url");
            startParsingTask(url); //receiving start
        }
    }
}

1 个答案:

答案 0 :(得分:0)

IntentService每次都处理一次调用,你不需要同步或其他任何东西。

Volley还支持多次调用,因此您根本不会遇到任何并发问题。

之后,如果您正在调用API,您可能需要尝试改进(http://square.github.io/retrofit/)或者至少使用GSON来反序列化JSON数据。