[英]Unhandled exception java.net.MalformedURLException in an Android app
我的代码有问题
void getItembyID(String code){
String site;
site = "http:/www.sprii.net/db/getItem.php?pid="+code+"/";
URL url = new URL(site);
}
在用IntelliJ编写时,第三行出现异常。 问题出在URL url = new URL(site);
我应该尝试/抓住它吗? 为什么会发生? 我尝试输入"http://www.google.com/"
以查看其是否适用,但仍然给我同样的例外
编辑:我意识到我在我的网站名称中有一个错误。 它在http:之后遗漏//。 但是,更改后,我仍然得到“ java.net.MalformedURLException”
我意识到了问题所在。 此错误意味着我正在调用的方法是使用throws
声明的,因此必须使用try{}catch(){}
或通过添加相同的throws
如果您应该在附加之前对它进行编码,请确保String code
没有任何空格
发送以下网址时,我遇到了类似的错误
字符串unEncodedUrl =“ https://yXXXXXXdh.execute-api.us-east-1.amazonaws.com/prod/helloWorld?date=2017年 12月29日”;
URL url =新URL(unEncodedUrl);
上一行已引发错误,因为日期url查询值中有空格
因此您可以通过对日期值进行编码来解决
字符串encodeUrl =“ https://yXXXXXXdh.execute-api.us-east-1.amazonaws.com/prod/helloWorld?date= ”“ + URLEncoder.encode(finaldate,” utf-8“);
注意:请勿对整个URL进行编码,而仅对包含空格的部分进行编码
您缺少/
尝试使用“ http://www.sprii.net/db/getItem.php?pid= ”
也许您应该扩展此功能,以便在出现更多错误时可以看到更多错误,例如下面的代码。 (顺便说一句,这为我执行,但是我不知道该参数的任何有效“代码”值)
public void getItembyID(String code) {
String site;
site = "http://www.sprii.net/db/getItem.php?pid=" + code + "/";
try {
URL url = new URL(site);
} catch (MalformedURLException e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
您在网址中缺少/。 应该是http://
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.