java - 如何创建单例 OkHtpp 类并处理以前的请求

标签 java android okhttp

我在 android 上使用 Google Autocomplete Places API,我想做的是:

  1. 每次用户在 EditText 中键入一个字符时,它都需要发出新请求以获取预测结果。
  2. 我需要取消之前的请求,因为我需要的唯一结果是用户输入的最终地址/位置。
  3. 然后,如果位置长度小于 3 个字符,则使用一些逻辑来清理 RecyclerView。

这就是为什么我需要一个单例实例,我试过这个:

public class OkHttpSingleton extends OkHttpClient {
private static  OkHttpClient client = new OkHttpClient();

public static OkHttpClient getInstance() {
    return client;
}

public OkHttpSingleton() {}

public void CloseConnections(){
    client.dispatcher().cancelAll();
}
public List<PlacePredictions> getPredictions(){
    //// TODO: 26/09/2017  do the request!
    return null;
}

但我不确定这样做是否正确,因为在 doc 中它说 dispatcher().cancelAll() 方法取消所有请求但我知道这种方式是错误的!我更关心如何创建单例,然后是其他。

主要 Activity :

Address.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if(s.length() > 3){
                _Address = Address.getText().toString();
                new AsyncTask<Void, Void, String>() {
                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();
                    }

                    @Override
                    protected String doInBackground(Void... params) {
                        try { // request...
                    }else{Clear the RecyclerView!}

最佳答案

您可以实现一个单例帮助程序类,它保留一个 OkHttpClient 并使用这个特定的客户端覆盖所有自定义功能:

public class OkHttpSingleton {

    private static OkHttpSingleton singletonInstance;

    // No need to be static; OkHttpSingleton is unique so is this.
    private OkHttpClient client;

    // Private so that this cannot be instantiated.
    private OkHttpSingleton() {
        client = new OkHttpClient.Builder()
            .retryOnConnectionFailure(true)
            .build();
    }

    public static OkHttpSingleton getInstance() {
        if (singletonInstance == null) {
            singletonInstance = new OkHttpSingleton();
        }
        return singletonInstance;
    }

    // In case you just need the unique OkHttpClient instance.
    public OkHttpClient getClient() {
        return client;
    }

    public void closeConnections() {
        client.dispatcher().cancelAll();
    }

    public List<PlacePredictions> getPredictions(){
        // TODO: 26/09/2017  do the request!
        return null;
    }
}

使用示例:

OkHttpSingleton localSingleton = OkHttpSingleton.getInstance();
...
localSingleton.closeConnections();
...
OkHttpClient localClient = localSingleton.getClient();
// or
OkHttpClient localClient = OkHttpSingleton.getInstance().getClient();

关于java - 如何创建单例 OkHtpp 类并处理以前的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46430416/

相关文章:

java - 使用 lambda 保护线程安全

Android 将值从 Tabs 传递到 Main TabActivity 的最佳方式

java - 在Android Studio中按下按钮时播放音频/声音问题

java 无法在 Windows 7 中创建新文件?

java - Android 和 Java 的 Retrofit REST 客户端不会终止

android - Kotlin MediaType.parse ("something")是一个错误。移至扩展功能

android - Wifi 使用改造和 okhttp 阻止来 self 的应用程序的网络调用

android - 改造 2 : Custom annotations for custom interceptor

java - 通过java程序远程连接mySQL

android - 单击单个项目选择回收站 View 中的所有项目