在我的Android应用程序中,我必须从URL(http://api.offersdb.com/distribution/beta/offers.json?api_key=demo&radius=10&postal_code=30305
)获取XML数据,因为我正在使用以下代码,
try {
URL url = new URL(urlStr);
URLConnection urlConn = url.openConnection();
if (!(urlConn instanceof HttpURLConnection)) {
throw new IOException("URL is not an Http URL");
}
HttpURLConnection httpConn = (HttpURLConnection) urlConn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
resCode = httpConn.getResponseCode();
if (resCode == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
}catch (Exception e) {
e.printStackTrace();
}
但它返回以下异常
java.net.UnknownHostException: Host is unresolved: api.offersdb.com:80
当我在URL中使用ipaddress(XX.XX.XXX.XX)更改主机名(api.offersdb.com)时,我可以获得响应。我不知道为什么我会遇到这个问题,请有人帮助我。
先谢谢,
Rajapandian
答案 0 :(得分:1)
您是否尝试在清单文件中设置以下权限?
<uses-permission android:name="android.permission.INTERNET" />
/ Alex
答案 1 :(得分:0)
这意味着在您的设备/模拟器上以某种方式破坏了名称解析。您是否可以在Android设备的内置浏览器中使用该网址?