Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.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/1/php/281.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
Java 将数据从php页面解析到android_Java_Php_Android_Xml_Parsing - Fatal编程技术网

Java 将数据从php页面解析到android

Java 将数据从php页面解析到android,java,php,android,xml,parsing,Java,Php,Android,Xml,Parsing,因此,我正在学习android,并尝试解析来自以下网站的数据: 我不确定这是什么格式。它似乎是XML,但与其他东西混合在一起?我曾尝试使用XMLParser和org.XML.sax解析器将其解析为XML,但遇到两个问题: 当我试图使用下面代码中的getUrlString()方法从指定的URL下载数据作为字符串,并使用logcat或toast将其可视化时,我只是在菱形中得到一堆问号字符 public byte[] getUrlBytes(String website) throws IOExce

因此,我正在学习android,并尝试解析来自以下网站的数据:

我不确定这是什么格式。它似乎是XML,但与其他东西混合在一起?我曾尝试使用XMLParser和org.XML.sax解析器将其解析为XML,但遇到两个问题:

  • 当我试图使用下面代码中的
    getUrlString()
    方法从指定的URL下载数据作为字符串,并使用logcat或toast将其可视化时,我只是在菱形中得到一堆问号字符

    public byte[] getUrlBytes(String website) throws IOException {
     URL url = new URL(website);
     HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
      try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream in = connection.getInputStream();
    
        if (connection.getResponseCode() != connection.HTTP_OK) {
          throw new IOException(connection.getResponseMessage());
        }
    
        int bytesRead = 0;
        byte[] buffer = new byte[1024];
        while ((bytesRead = in.read()) > 0) {
          out.write(buffer, 0, bytesRead);
        }
        out.close();
    
        return out.toByteArray();
      } finally {
        connection.disconnect();
      }
    }
    
    public String getUrlString(String website) throws IOException {
      return new String(getUrlBytes(website));
    }
    
  • 我遇到的另一个问题是,当我试图将结果解析为XML时,会出现一个错误,说它不是有效的XML。我想这些问号毕竟不是有效的XML,但我尝试通过XML验证器运行上面的URL,但仍然说它不是有效的XML

  • 再说一次,这是什么样的数据,我如何解析它呢?

    试试看

    下面是从页面获取数据的示例

    import java.io.IOException;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    public class ExtractInfo  {
    
       public static void main (String [] args) throws IOException{ 
    
           Document doc = Jsoup.connect("http://www.brimfieldfleafinder.com/dealersservc.php").get(); //get the page
           Elements dealersList = doc.select("dealer"); // select all dealer tags
           for (Element dealer : dealersList){     
               Elements dealerInfos = dealer.children();   // for each dealer tag select all child tags
               for(Element inf : dealerInfos){
                   System.out.println(inf.tagName() +" : "+ inf.text());  //print tag name and if exists tag text
               }
               System.out.println("********************************");
           }
       }
    }
    

    它不是XML,它只是一个由分隔符行分隔的列表,所以我如何解析/使用数据?如果使用浏览器检查器,您可以看到由标记分隔的数据,例如它不是有效的XML,但看起来确实可以使用它。它似乎是用HTML包装的HTML。您可以尝试从字符串中删除所有标记,例如。然后将正确的XML标记添加到剩余字符串的开头和结尾。然后,您可以将这个新字符串加载到XML解析器中。在异步任务上运行它,并将数据解析为模型对象。非常感谢!