1

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

我正在使用以下代码:

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("")) {
    ...
    }
   }

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

4

0 回答 0