我正在使用 android 2.3.3,我制作了一个运行良好的 rss 阅读器,然后我将那个简单的 rss 阅读器的代码集成到另一个活动中,只需小心复制粘贴,那里没有错误。
问题是当我在我的模拟器上运行应用程序时,它给了我连接异常的错误。然后我通过在 try 块中的每一行之后放置敬酒来发现问题出httpconnection.connect();
在行。
我在 android 清单中添加了权限,而我的 logcat 给出了javaio exception:error connecting
.
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
//Toast.makeText(this, "urlconnection passed", Toast.LENGTH_SHORT).show();
httpURLConnection.setAllowUserInteraction(false);
httpURLConnection.setInstanceFollowRedirects(true);
//Toast.makeText(this, "setting response method", Toast.LENGTH_SHORT).show();
httpURLConnection.setRequestMethod("GET");
//Toast.makeText(this, "connecting", Toast.LENGTH_SHORT).show();
httpURLConnection.connect();
Toast.makeText(this, "on response code", Toast.LENGTH_SHORT).show();
response = httpURLConnection.getResponseCode();
//Toast.makeText(this, "response code passed", Toast.LENGTH_SHORT).show();
if (response == HttpURLConnection.HTTP_OK) {
//Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show();
inputStream = httpURLConnection.getInputStream();
}
} catch (Exception e) {
// TODO: handle exception
throw new IOException("Error connecting");
}
这里是创建对象的代码:
URL url = new URL(UrlString);
URLConnection connection = url.openConnection();
if (!(connection instanceof HttpURLConnection))
throw new IOException("Not a HTTP connection");