如何修复Android中无效的网址或端口错误?

问题描述:

String str1="http://10.0.2.2/moodle"; 
String str4="admin"; 
String str5="[email protected]"; 

try{ 
    String getToken = str1 + "/login/token.php?username=" + str4 + "&password=" + str5 + "&service=moodle_mobile_app"; 
    String inputLine = null; 
    URL url = new URL(getToken.toString()); 
    System.out.println("Port----------->"+url.getPort()); 
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
    while ((inputLine = in.readLine()) != null) { 
     System.out.println("test1"+inputLine); 
    } 
    in.close(); 
} catch(Exception e) { 
    System.out.println(e.toString()); 
} 
} 

当我尝试从localhost moodle course web获取令牌时,我的端口号为-1。 我得到这个错误“:”无效的网址或端口。对于上面的代码 请帮我解决这个错误。如何修复Android中无效的网址或端口错误?

+1

你为什么要在getToken上调用toString?这已经是一个字符串... – Matthieu 2013-03-06 17:56:11

你可能想看看URI constructors并使用它们。手动建立是一个不错的方法。

我有同样的问题。为了解决这个问题,我不得不编辑位于Moodle的根文件夹中的文件config.php内以下行(/var/www/moodle/config.php在我的情况,在Ubuntu 12.10):

$CFG->wwwroot = 'http://10.0.2.2/moodle'; 

你将不得不使用的IP地址,你的设备将作为Moodle Web服务器的主机(在仿真器的情况下,主机看在10.0.2.2)。