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();
}
}
当我使用上面的代码时,在HttpURLConnection中,url变为红色并且Android Studio显示错误无法解析符号URL 。代码有什么问题?
答案 0 :(得分:15)
我遇到了同样的问题。只是做:
import java.net.URL;
答案 1 :(得分:5)
将打开连接的行放在try
子句中:
try {
URL url = new URL(UR);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
} catch(MalformedURLException e) {
e.printStackTrace();
}
这是因为有价值的url
是本地的try
仅在url
子句内有效。
或者在try
子句之外声明URL url;
try {
url = new URL(UR);
} catch(MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
:
Robot
答案 2 :(得分:0)
有时你需要一个简单的'gradlew clean'
"Click" on "Build"->"Clean Project" and that will perform a gradle clean
或者:
"Tools" -> "Android" -> "Sync Project with Gradle Files"
答案 3 :(得分:-1)
添加
import java.net.MalformedURLException;
解决缺少的决心符号。
你当然也必须拥有这个:
import java.net.URL;