[英]Cannot connect using httpconnection in android
我正在使用android 2.3.3,我已經制作了一個工作得很好的rss閱讀器,然后我將那個簡單的rss閱讀器的代碼集成到另一個活動中,只需復制粘貼它,沒有錯誤。
問題是當我在我的模擬器上運行應用程序時,它給出了連接異常的錯誤。 然后我通過在try block中的每行之后放置toast來解決問題是在httpconnection.connect();
線。
我在android清單中添加了權限,而我的logcat發出了javaio exception:error connecting
的警告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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.