簡體   English   中英

使用Jsoup在Eclipse中解析網頁

[英]Parsing a webpage in Eclipse using Jsoup

嗨,我是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代碼中沒有什么特別的。 當我在虛擬設備上運行該應用程序時,除了空白頁面外,什么都沒有。

任何幫助或建議,將不勝感激,請記住,我是新來的。

謝謝,

請使用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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM