Android-设置加载程序超时

Android-设置加载程序超时,android,timeout,android-loadermanager,android-loader,android-internet,Android,Timeout,Android Loadermanager,Android Loader,Android Internet,我正在使用加载程序处理互联网连接,它工作正常。现在我想控制连接尝试的持续时间,换句话说,设置超时 我正在使用以下代码: 在onCreate方法中: Uri SearchUri = Uri.parse(urlString); Bundle args = new Bundle(); args.putParcelable(ARGS_URI, SearchUri); // Initialize the Loader.

我正在使用加载程序处理互联网连接,它工作正常。现在我想控制连接尝试的持续时间,换句话说,设置超时

我正在使用以下代码:

onCreate
方法中:

Uri SearchUri =
                Uri.parse(urlString);

        Bundle args = new Bundle();
        args.putParcelable(ARGS_URI, SearchUri);

        // Initialize the Loader.
        getLoaderManager().initLoader(LOADER_SEARCH,
                args, this);
其他方法:

    @Override
public Loader<RESTLoader.RESTResponse> onCreateLoader(int i, Bundle args) {

    a = args;

    Log.v("TESTING","in OnCreateLoader");
    if (args != null && args.containsKey(ARGS_URI)){

        Log.v("TESTING","in OnCreateLoader, getting communication key");
        Uri action = args.getParcelable(ARGS_URI);

        return new RESTLoader(this, RESTLoader.HTTPVerb.GET,action);
    }



    return null;
}


  @Override
public void onLoadFinished(Loader<RESTLoader.RESTResponse> loader, RESTLoader.RESTResponse data) {



    int code = data.getCode();
    String json = data.getData();


    if (code == 200){
        Log.v("TESTING","Inside onLoadFinished, code = 200");
    }

    else {
        Log.v("TESTING","Inside onLoadFinished, code != 200");
        Toast.makeText(getApplicationContext(), "Connection Problem", Toast.LENGTH_SHORT).show();
    }


    // Check to see if we got an HTTP 200 code and have some data.
    if (code == 200 && !json.equals("")) {
    ...
    }
   }
@覆盖
公共加载器onCreateLoader(int i,Bundle args){
a=args;
Log.v(“测试”,“在OnCreateLoader中”);
if(args!=null&&args.containsKey(args_URI)){
v(“测试”,“在OnCreateLoader中,获取通信密钥”);
Uri action=args.getParcelable(args_Uri);
返回新的RESTLoader(这个,RESTLoader.HTTPVerb.GET,action);
}
返回null;
}
@凌驾
public void onLoadFinished(加载程序、RESTLoader.restreponse数据){
int code=data.getCode();
字符串json=data.getData();
如果(代码==200){
Log.v(“测试”,“内部加载完成,代码=200”);
}
否则{
Log.v(“测试”,“内部加载完成,代码!=200”);
Toast.makeText(getApplicationContext(),“连接问题”,Toast.LENGTH\u SHORT.show();
}
//检查一下我们是否有HTTP 200代码和一些数据。
if(code==200&&!json.equals(“”){
...
}
}

那么,如何在加载程序上设置超时?例如,给出一个10秒的持续时间,如果没有响应,停止尝试。

你是否考虑使用AcycQueHealdLoad?这对积垢有好处operations@pskink实际上我还没有,但我想先检查一下是否可以使用加载器,因为我在我的应用程序中广泛使用加载器。切换到其他方法会很麻烦。您是否尝试通过延迟处理程序消息调用cancelLoad?@pskink您可以详细说明这一点吗?在AsyncTaskLoader的构造函数或onStartLoading中创建一个处理程序:h=new Handler(this),然后在handleMessage方法调用cancelLoad中调用h.sendEmptyMessageDelayed(0,tout)