我有一个异步任务,用于在启动时执行的活动。如果用户手动选择一个位置,则该URL包含一个作为参数传递给asynctask的值,并且如果用户未指定位置,则async任务将使用默认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);
}
最佳答案
尝试if(url.length ==0)
检查它是否为空!