使用Jsoup在Eclipse中解析网页

时间:2013-10-15 02:33:57

标签: java android eclipse parsing html-parsing

您好我是Eclipse和编码的新手,并且一直在尝试制作一个简单的应用来提高我的技能。我尝试从我的应用程序中的网页重现表格时遇到了一些麻烦。我花了几个小时咨询论坛如何这样做,但它似乎没有用。我试图使用JSoup解析页面。我已经下载并导入了Jsoup。这是我目前的java:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Standingspage extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_standingspage);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.standingspage, menu);
    return true;
}

public static void main(String[] args) throws Exception {
    Document doc = Jsoup.connect("http://pcihl.ca/Statistics/RegularSeasonStandings").get();
    for (Element table : doc.select("table")) {
        for (Element row : table.select("tr")) {
            Elements tds = row.select("td");
            System.out.println(tds.get(0).text());   
        }
    }
}

    }

除了一些布局信息之外,我在相关的xml代码中没有任何特殊之处。 当我在虚拟设备上运行应用程序时,我只得到一个空白页面。

非常感谢任何帮助或建议,请记住我是新手。

谢谢,

1 个答案:

答案 0 :(得分:0)

请使用kso​​ap2-android库..


示例: -


private static final String NAMESPACE = YOUR_URL_IN_STRING;
    private static final String HEADER = "<?xml version='1.0' encoding='UTF-8' standalone='no'?>";
    private static final int SOAP_VERSION = SoapEnvelope.VER11;
方法

中的

long startTime=System.currentTimeMillis();
    String METHOD_NAME = "CheckDeviceRegistration";
    String SOAP_ACTION = NAMESPACE + METHOD_NAME;

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("deviceCode", deviceCode);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SOAP_VERSION); // put all required data into a soap envelope
    envelope.dotNet = true;
    // envelope.addMapping(NAMESPACE, "GetApplicationConfigurations", GetApplicationConfigurations.class);
    // envelope.headerOut = security; // this is an Element[] created before
    // envelope.encodingStyle = SoapEnvelope.ENC;
    envelope.setAddAdornments(false);
    envelope.implicitTypes = false;
    envelope.setOutputSoapObject(request); // prepare request

    HttpTransportSE httpTransport = new HttpTransportSE(URL, timeOut);

    httpTransport.debug = DEBUG; // this is optional, use it if you don't want to use a packet sniffer to check what the sent
                                    // message was (httpTransport.requestDump)
    httpTransport.setXmlVersionTag(HEADER);

    try {

        httpTransport.call(SOAP_ACTION, envelope);

    } catch (IOException e) {

        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } // send request
    Log.d("RAM RAM3", "XML: " + httpTransport.requestDump);
    SoapObject result = null;
    String returnString = "";
    try {
        envelope.getResponse();

        result = (SoapObject) envelope.bodyIn;
        returnString = result.getProperty(0).toString();
    } catch (Exception e) {
        e.printStackTrace();
    }