【发布时间】:2015-06-04 16:34:34
【问题描述】:
我的网址格式有问题。我想从 url 发出一个 get 请求: 'http://www.radiobébé.com/titre.txt'(http://www.radiobebe.com/titre.txt 不存在)
String responseBody = "";
try {
URL u = new URL("http://www.radiobébé.com/titre.txt");
HttpURLConnection urlConnection = (HttpURLConnection) u
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
int statusCode = urlConnection.getResponseCode();
if (statusCode == HttpURLConnection.HTTP_OK) {
responseBody = urlConnection.getInputStream().toString();
}
} catch (IOException e) {
Log.e("RadioBébé", "failed to get song title : " + e.toString());
}
使用这个函数我得到错误'java.net.UnknownHostException'
我试过了:
String url = "http://www." + URLencoder.encoder("radiobébé", "UTF-8") + ".com/titre.txt";
和
Uri.Builder builder = new Uri.Builder();
builder.scheme("http")
.authority("www.radiobébé.com")
.appendPath("titre.txt");
同样的问题....(主机可能不为空)
你知道我该如何解决吗?
【问题讨论】:
标签: android uri httpurlconnection urlencode