2015-04-15 105 views
-2

我遇到了(在我看来)真的很奇怪的问题。我在Android Studio中有一个包含以下代码的Android项目:Android跳过代码

if(AppSettings.isNetworkAvailable(context, showDialog)) { 
return null; 
} 
else { 

    HttpResponse httpResponse = null; 
    AsyncHttpGet asyncHttpGet = new AsyncHttpGet(mHttpClient, mHttpContext); 

    String url = BASE_URI + atPath; 
    asyncHttpGet.execute(url); 

    try { 
     httpResponse = asyncHttpGet.get(); 
     System.out.println("Response: " + httpResponse); 
    } catch (InterruptedException | ExecutionException e) { 
     e.printStackTrace(); 
    } 

    if(isAccepted(httpResponse)) { 
     return httpResponse; 
    } else { 
     return null; 
    } 
} 

此代码在运行时返回null,并且不提供输出。调试器光标从第一个if子句(返回true)直接跳转到最后一个return语句,而不声明或初始化任何变量。

我也尝试删除其他,因为它应该没有它的工作,但这没有什么区别。有没有人有一个想法可以解决问题?

编辑:我应该补充:代码工作得很好,没有初始的if-clause并返回一个有效的HttpResponse。

回答

2

如果AppSettings.isNetworkAvailable(context, showDialog)为真,return null是正确的行为。

如果你想进入其他部分,使用:

if(AppSettings.isNetworkAvailable(context, showDialog)) { 
    HttpResponse httpResponse = null; 
    AsyncHttpGet asyncHttpGet = new AsyncHttpGet(mHttpClient, mHttpContext); 

    String url = BASE_URI + atPath; 
    asyncHttpGet.execute(url); 

    try { 
     httpResponse = asyncHttpGet.get(); 
     System.out.println("Response: " + httpResponse); 
    } catch (InterruptedException | ExecutionException e) { 
     e.printStackTrace(); 
    } 

    if(isAccepted(httpResponse)) { 
     return httpResponse; 
    } else { 
     return null; 
    } 
} 
+1

你错过了一个return语句 – Blackbelt

+1

谢谢,真有“问题”! 3个人,1小时的调试,没有人看到......我猜测调试器跳跃只是由于一些优化返回语句。 –