如何使用Jsoup将数据发布到ajax函数

时间:2013-04-27 15:18:19

标签: java ajax parsing jsoup

我想将字符串发布到

<li id="coz"><a
        onclick="doRequest('zemberek.jsp','YAZI_COZUMLE');">Cozumle</a></li>
是谁?Jsoup?我怎么办? 这是原始网站:http://zemberek-web.appspot.com/

<html>
<head>
    <script>
        function doRequest(url, islem) {
            var ajaxRequest = new AjaxRequest(url);
            var hiddenField = document.getElementById("islem");
            hiddenField.value = islem;
            ajaxRequest.addNamedFormElements("giris", "islem");
            ajaxRequest.sendRequest();
        }
    </script>
</head>

<body>
<big>Zemberek Demo</big>
<small>(<a href="http://code.google.com/p/zemberek">Zemberek
Proje Sitesi</a>)</small>
<div id="menu">
<ul id="nav">
    <li id="denetle"><a
        onclick="doRequest('zemberek.jsp', 'YAZI_DENETLE');">Denetle</a></li>
    <li id="coz"><a
        onclick="doRequest('zemberek.jsp','YAZI_COZUMLE');">Cozumle</a></li>
    <li id="oner"><a onclick="doRequest('zemberek.jsp','ONER');">Oner</a></li>
    <li id="ascii2tr"><a
        onclick="doRequest('zemberek.jsp','ASCII_TURKCE');">Ascii->Tr</a></li>
    <li id="tr2ascii"><a
        onclick="doRequest('zemberek.jsp','TURKCE_ASCII');">Tr->ascii</a></li>
    <li id="hecele"><a onclick="doRequest('zemberek.jsp','HECELE');">Hecele</a></li>
    <li id="ayristir"><a
        onclick="doRequest('zemberek.jsp','SACMALA');">Sacmala</a></li>
</ul>
</div>


<br>
<br>
<br>
<br>
<br>

<form id="form" action="#">
<P align=center><b>Islem yapilacak yaziyi asagidaki alana
giriniz.</b><br>
<textarea name="giris" rows="10" cols="60"></textarea> <input
    type="hidden" name="islem" id="islem" /></P>
</form>

<br>

<div id="div"></div>

</body>
</html>

2 个答案:

答案 0 :(得分:2)

使用Jsoup的简单而有效的解决方案:

<强>代码

String url = "http://zemberek-web.appspot.com/zemberek.jsp?ts=1367326940830&giris=%s&islem=YAZI_COZUMLE";

String query = "MyParamĄĘÓŚŁ";

String formattedUrl = String.format(url, URLEncoder.encode(query, "UTF-8"));

Document document = Jsoup.connect(formattedUrl).get();

String result = document.select("taconite-root > taconite-replace-children > div").text();

System.out.println(result);

<强>结果

MyParam :cozulemedi

答案 1 :(得分:0)

如果您在Google Chrome开发工具中查看请求,我认为以下内容如下所示,您会看到当您点击生成的网址时,例如:

http://zemberek-web.appspot.com/zemberek.jsp?ts=1367076182039&giris=bnfhjfttgfhffgfg&islem=ASCII_TURKCE

giris = bnfhjfttgfhffgfg =&gt;是你的字符串发送到服务器。

所以你可以使用以下每种编程语言

http://zemberek-web.appspot.com/zemberek.jsp?ts=1367076182039&giris=MY_STRING&islem=ASCII_TURKCE

不要忘记用UTF-8编码字符串作为查询字符串

<强>更新

这是我做过的一个例子

public class MyRequester {

    /**
     * @param args
     */
    public static void main(String[] args) {

        HttpURLConnection conn = null;
        InputStream in = null;

        try {


            String textToSend = "Java is cool :)";
            String urlRequest = "http://zemberek-web.appspot.com/zemberek.jsp?ts=1367076182039&giris="+URLEncoder.encode(textToSend, "UTF-8")+"&islem=ASCII_TURKCE";

            System.out.println(urlRequest+"\n");

            conn = (HttpURLConnection) new URL(urlRequest).openConnection();
            in = conn.getInputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder sb = new StringBuilder();
            String data = null;

            while ((data = reader.readLine()) != null) {
              sb.append(data);
            }

            System.out.println(sb.toString());


        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {

            if(conn != null){
                conn.disconnect();
            }

            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }



    }

}

控制台中的输出:

http://zemberek-web.appspot.com/zemberek.jsp?ts=1367076182039&giris=Java+is+cool+%3A%29&islem=ASCII_TURKCE

<taconite-root> <taconite-replace-children  contextNodeID="div" parseInBrowser="true"><div> Java <font color="#33AA33">iÅŸ</font> <font color="#FF0033">cool</font> :) </div> </taconite-replace-children> </taconite-root>

请求的结果是XML文档。根据我的经验,我会使用SAX而不是Java XML本机实现。