Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Jsoup将带有多个div的html解析器从swift转换为android_Android_Swift_Html Parsing_Jsoup_Tfhpple - Fatal编程技术网

使用Jsoup将带有多个div的html解析器从swift转换为android

使用Jsoup将带有多个div的html解析器从swift转换为android,android,swift,html-parsing,jsoup,tfhpple,Android,Swift,Html Parsing,Jsoup,Tfhpple,我正在尝试将iOS应用程序转换为android。但我几天前才开始学习Java。我试图从html中的标记中获取一个值 这是我的swift代码: if let url = NSURL(string: "http://www.example.com/") { let htmlData: NSData = NSData(contentsOfURL: url)! let htmlParser = TFHpple(HTMLData: htmlData) /

我正在尝试将iOS应用程序转换为android。但我几天前才开始学习Java。我试图从html中的标记中获取一个值

这是我的swift代码:

if let url = NSURL(string: "http://www.example.com/") {
        let htmlData: NSData = NSData(contentsOfURL: url)!
        let htmlParser = TFHpple(HTMLData: htmlData)


        //the value which i want to parse
        let nPrice = htmlParser.searchWithXPathQuery("//div[@class='round-border']/div[1]/div[2]") as NSArray

        let rPrice = NSMutableString()

        //Appending
        for element in nPrice {
            rPrice.appendString("\n\(element.raw)")
        }
        let raw = String(NSString(string: rPrice))

        //the value without trimming    
        let stringPrice = raw.stringByReplacingOccurrencesOfString("<[^>]+>", withString: "", options: .RegularExpressionSearch, range: nil)

        //result
        let trimPrice = stringPrice.stringByReplacingOccurrencesOfString("^\\n*", withString: "", options: .RegularExpressionSearch)
}
我的问题如下:

  • 每当我尝试任何代码时,我都会遇到NetworkOnMainThreateException
  • 我不确定使用此结构的getElementByTag是否正确
  • 请帮忙, 谢谢

  • 每当我尝试任何代码时,我都会遇到NetworkOnMainThreateException
  • 你应该使用截击而不是Jsoup。这将是一个更快、更有效的替代方案。有关示例代码,请参见

  • 我不确定使用此结构的getElementByTag是否正确
  • Jsoup不理解xPath。它与CSS选择器一起工作。 上述代码行可以按如下方式更正:

    Elements divs = doc.select("div.round-border > div:nth-child(1) > div:nth-child(2)");
    
    for(Element div : divs) {
        // Process each div here...
    }
    

    你有什么问题。每当我尝试任何代码时,我都会遇到NetworkOnMainThreateException。2.我不确定使用此结构的getElementByTag是否正确。能否在帖子中添加异常stacktrace?您认为使用Volley可以解决此异常吗?这是由于试图从主线程而不是从服务/任务访问网络造成的,不是吗?@TDG在主线程中执行网络IO被认为是不好的做法。这就是为什么我建议在这里截击。即使是截击,也可能出现例外情况。因为没有任何stacktrace我不能再说什么了。。。
    Element content = doc.getElementsByTag("//div[@class='round-border']/div[1]/div[2]");
    
    Elements divs = doc.select("div.round-border > div:nth-child(1) > div:nth-child(2)");
    
    for(Element div : divs) {
        // Process each div here...
    }