Android 检查AsyncTask参数是否为空

Android 检查AsyncTask参数是否为空,android,android-asynctask,indexoutofboundsexception,Android,Android Asynctask,Indexoutofboundsexception,对于启动时执行的活动,我有一个异步任务。如果用户手动选择一个位置,该url包含一个作为参数传递给异步任务的值,并且如果用户不指定位置,异步任务将使用默认url。我的问题是,如果没有指定参数,我无法对代码调用url[0]。是否有方法检查并查看参数是否已传递到异步任务中?下面是我的尝试。我尝试了url[0].isEmpty()和url[0]==null,但两者都给了我IndexOutOfBounds错误 private class CallDestination extends AsyncTask&

对于启动时执行的活动,我有一个异步任务。如果用户手动选择一个位置,该url包含一个作为参数传递给异步任务的值,并且如果用户不指定位置,异步任务将使用默认url。我的问题是,如果没有指定参数,我无法对代码调用
url[0]
。是否有方法检查并查看参数是否已传递到异步任务中?下面是我的尝试。我尝试了
url[0].isEmpty()
url[0]==null
,但两者都给了我
IndexOutOfBounds
错误

private class CallDestination extends AsyncTask<String, Void, JSONObject>{
        protected JSONObject doInBackground(String...url){

            String MYURL = "";

            if(url[0].isEmpty()){
                MYURL = "http://thevisitapp.com/api/destinations/read?identifiers=10011";
            } else{
                MYURL = "http://thevisitapp.com/api/destinations/read?identifiers=" + url[0];
            }
            //TODO make this dynamic as it's passed in from other activity through intent


            HttpRequest request = new HttpRequest();
            return request.getJSONFromUrl(MYURL);
}
私有类CallDestination扩展异步任务{
受保护的JSONObject doInBackground(字符串…url){
字符串MYURL=“”;
如果(url[0].isEmpty()){
MYURL=”http://thevisitapp.com/api/destinations/read?identifiers=10011";
}否则{
MYURL=”http://thevisitapp.com/api/destinations/read?identifiers=“+url[0];
}
//TODO在通过intent从其他活动传入时使其动态化
HttpRequest请求=新建HttpRequest();
return request.getJSONFromUrl(MYURL);
}

尝试
如果(url.length==0)
检查它是否为空!

您可以通过以下方式检查是否提供了参数:

if(url == null)

检查
if(url[0].length==0)时,您会得到ArrayIndexOutOfBoundsException
,因为
url
null
,这基本上意味着数组包含0个元素,这也意味着您无法访问
url[0]
由于被认为超出范围

您是否知道
字符串中的
字符串…
url
是一个数组?因此,您可以执行以下几级检查:

  • 检查
    url
    是否为空:

    if(url.length() == 0){
        //error, no URL given
    }
    
  • 检查
    url
    是否有值,但为空:

    if(url[0] == null){
        //error, url is null
    }