[英]Cannot Resolve Symbol error when using URL
class background_thread extends AsyncTask<String ,String , Boolean > {
protected Boolean doInBackground(String... params) {
String UR = "127.0.0.1/abc/index.php";
try {
URL url = new URL(UR);
} catch(MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
}
}
When I use the above code, in the HttpURLConnection the url turns red and Android Studio is showing an error can not resolve symbol url .当我使用上面的代码时,在 HttpURLConnection 中 url 变为红色并且 Android Studio 显示错误无法解析符号 url 。 What's wrong with the code?代码有什么问题?
I encountered the same problem.我遇到了同样的问题。 Just do:做就是了:
import java.net.URL;
Put the line which is openning connection inside of try
clause:将打开连接的行放在try
子句中:
try {
URL url = new URL(UR);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// Do something...
} catch (IOException e) {
e.printStackTrace();
} finally {
conn.disconnect();
}
It is because the valiable url
is a local one which is valid only inside the try
clause.这是因为有效的url
是一个本地的,只在try
子句中有效。
Or declair the url
outside of the try
clause:或者在try
子句之外声明url
:
URL url;
try {
url = new URL(UR);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// Do something...
} catch (IOException e) {
e.printStackTrace();
} finally {
conn.disconnect();
}
With Java 7+, we can use the AutoClosable feature:在 Java 7+ 中,我们可以使用AutoClosable功能:
URL url;
try {
url = new URL(UR);
} catch(MalformedURLException e) {
e.printStackTrace();
}
try (HttpURLConnection conn = (HttpURLConnection) url.openConnection())
// Do something...
} catch (IOException e) {
e.printStackTrace();
}
Sometimes you need a asimple 'gradlew clean'有时你需要一个简单的“gradlew clean”
"Click" on "Build"->"Clean Project" and that will perform a gradle clean
Or:或者:
"Tools" -> "Android" -> "Sync Project with Gradle Files"
Adding添加
import java.net.MalformedURLException;
solve the missing resolve symbol.解决缺少的解析符号。
You must of course have this too:你当然也必须有这个:
import java.net.URL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.