2016-12-30 52 views
-1

在下面的代碼中,我正在接受一個I​​P,並根據命令給出一個GET請求。一些GET請求具有修飾符,這些修飾符是我可以用另一個GET請求獲取到相同URL的數字。什麼是確保這些修飾符有效的好方法,以便程序不會掛起?控制檯應用程序中的錯誤處理

public class UrlTanslatorInstance { 

public String ip; 

public UrlTanslatorInstance(String ip){ 
    this.ip = ip; 
} 

public String translate(String command, String modifier) throws MalformedURLException, IOException{ 
    String url; 

    switch (command.toUpperCase()){ 

     case "LEFT": 
      return "http://" + ip + "keypress/left"; 
     case "RIGHT": 
      return "http://" + ip + "keypress/right"; 
     case "KEYUP": 
      return "http://" + ip + "keyup/" + modifier; 
     case "KEYDOWN": 
      return "http://" + ip + "keydown/" + modifier; 

     case "KEYPRESS": 
      return "http://" + ip + "keypress/" + modifier; 

     case "APPIDS": 
      return "http://" + ip + "query/apps"; 

     case "LAUNCH": 
      return "http://" + ip + "keypress/left"; 

     case "ACTIVEAPP": 
      return "http://" + ip + "query/active-app"; 

    } 
    return "Command Not Valid"; 
} 

}

回答

0

你的意思是你如何可以處理在不屬於你定義那些開關的情況下?

使用default關鍵字來處理未定義的情況。

switch (foo){ 
case a: 
    doSomething(); 
    break; 
case b: 
    doSomethingElse(); 
    break; 
default: 
    handleUndefinedCases(); 
    break; 
} 
+0

不,對不起,不清楚。我的意思是說,當一個無效的修飾符被使用時,HTTP請求會永久掛起,並且我想在這之前就開始阻止它。無效的修飾符由可從服務器檢索的一組ID確定。 –

相關問題