我想下载XML文件进行解析?

时间:2012-11-20 19:53:41

标签: android xml-parsing

我正在使用以下代码下载文件表格接收来源

  private InputStream downloadUrl(String urlString) throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000 /* milliseconds */);
    conn.setConnectTimeout(15000 /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query  
    Log.v("Start Query", "Stream");
    conn.connect();
    Log.v("End Query", "Stream");
    InputStream stream = conn.getInputStream();    
    Log.v(stream.toString(), "Stream");
    return stream;
}

我在日志中得到了以下错误

11-09 12:39:59.386:D / SntpClient(73):请求时间失败:java.net.SocketException:协议不支持的地址族

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

      URL url = new URL(urlString);
      HttpURLConnection conn = (HttpURLConnection)url.openConnection();
      conn.setRequestMethod("GET");
      conn.setDoInput(true);
      conn.setReadTimeout(10000);
      conn.setConnectTimeout(15000);

      Log.v("Start Query", "Stream");          
      conn.connect();
      Log.v("End Query", "Stream");
      //read the result from the server
      BufferedReader rdr  = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      StringBuilder sbr = new StringBuilder();

      while ((line = rdr.readLine()) != null)
      {
          sbr.append(line + '\n');
      }

      Log.v(sbr.toString(), "Stream");