Android使用表单发布html数据

时间:2014-03-10 14:02:44

标签: php android html forms

我创建了一个接收3个文本字段的应用程序,它需要将数据发送到网站。我看过主页的php代码,因为我对php不太好,我不确定如何发布数据。以下是表单在网站上的显示方式:

<div id="tzgb-homesearch-wrap">
    <div id="tzgb-homesearch">
                <form method="post" action="http://www.online-bustickets.de/bus-departure-destination/">
        <input type="hidden" id="_searchbox" name="_searchbox" value="77e642a333"><input type="hidden" name="_wp_http_referer" value="/">            <div class="search-row">
            <label for="von">Von</label>
            <span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span><input type="text" id="von" name="von" placeholder="Abfahrtsort eingeben" class="ui-autocomplete-input" autocomplete="off" mouseev="true" keyev="true" style="background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABHklEQVQ4EaVTO26DQBD1ohQWaS2lg9JybZ+AK7hNwx2oIoVf4UPQ0Lj1FdKktevIpel8AKNUkDcWMxpgSaIEaTVv3sx7uztiTdu2s/98DywOw3Dued4Who/M2aIx5lZV1aEsy0+qiwHELyi+Ytl0PQ69SxAxkWIA4RMRTdNsKE59juMcuZd6xIAFeZ6fGCdJ8kY4y7KAuTRNGd7jyEBXsdOPE3a0QGPsniOnnYMO67LgSQN9T41F2QGrQRRFCwyzoIF2qyBuKKbcOgPXdVeY9rMWgNsjf9ccYesJhk3f5dYT1HX9gR0LLQR30TnjkUEcx2uIuS4RnI+aj6sJR0AM8AaumPaM/rRehyWhXqbFAA9kh3/8/NvHxAYGAsZ/il8IalkCLBfNVAAAAABJRU5ErkJggg==); padding-right: 0px; background-attachment: scroll; cursor: auto; background-position: 100% 50%; background-repeat: no-repeat no-repeat;">
            <input type="hidden" id="von-permalink">
        </div>
        <div class="search-row">
            <label for="nach">Nach</label>
            <span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span><input type="text" id="nach" name="nach" placeholder="Zielort eingeben" class="ui-autocomplete-input" autocomplete="off">
            <input type="hidden" id="nach-permalink">
        </div>
        <div class="search-row">
            <label for="datum">Datum</label>
            <input type="text" id="datum" name="datum" readonly="readonly" class="hasDatepicker">
        </div>
        <div class="submit">
            <input type="submit" id="tzgb-homesearch-submit" value="Suchen">
        </div>
    </form>
            </div>
</div>

我的应用需要使用在文本字段中输入的值提交此表单。我遇到的问题是它在提交时不仅使用script.php文件,而是使用类中的函数。这是来自home.php的代码:

<div id="tzgb-homesearch-wrap">
    <div id="tzgb-homesearch">
        <?php if ( class_exists( 'tzgb_busroute' ) ) global $tzgb_page_template; $tzgb_page_template->homeSearch(); ?>
    </div>
</div>

以下是函数homeSearch()的代码:

function homeSearch() {
    ?>
    <form method="post" action="<?php bloginfo( 'url' ) ?>/bus-departure-destination/">
        <?php wp_nonce_field( $this->nonceKey, $this->nonceField ); ?>
        <div class="search-row">
            <label for="von">Von</label>
            <input type="text" id="von" name="von" placeholder="Abfahrtsort eingeben" />
            <input type="hidden" id="von-permalink" />
        </div>
        <div class="search-row">
            <label for="nach">Nach</label>
            <input type="text" id="nach" name="nach"  placeholder="Zielort eingeben"/>
            <input type="hidden" id="nach-permalink" />
        </div>
        <div class="search-row">
            <label for="datum">Datum</label>
            <input type="text" id="datum" name="datum" readonly="readonly" />
        </div>
        <div class="submit">
            <input type="submit" id="tzgb-homesearch-submit" value="Suchen" />
        </div>
    </form>
    <?php
}

那么,我怎样才能将数据发布到网站上?我需要它来返回一个网页,然后将其显示在WebView中。

更新 我的代码现在看起来像这样:

HttpClient httpClient = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),
                10000);
        HttpConnectionParams.setSoTimeout(httpClient.getParams(), 10000);
        HttpPost httpPost = new HttpPost(
                "http://www.online-bustickets.de/bus-departure-destination/");
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("_searchbox",
                "5624dd9868"));
        nameValuePairs.add(new BasicNameValuePair("_wp_http_referer", "/"));
        nameValuePairs.add(new BasicNameValuePair("von", "Berlin"));
        nameValuePairs.add(new BasicNameValuePair("nach", "Alsfeld"));
        nameValuePairs.add(new BasicNameValuePair("datum", "12.03.2014"));
        // etc...
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httpPost);

            content = EntityUtils.toString(response.getEntity());



            Log.i("Test", "" + response.getStatusLine().getStatusCode());

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

但是当我尝试在webview中打开它时,我得到一个页面,说von和nach的值不在数据库中。有没有人有任何想法可能是什么问题?

UPDATE1 当您在不输入任何值的情况下进行搜索时,我会收到您在网站上收到的错误消息。这是与错误的链接:http://www.online-bustickets.de/bus--/

1 个答案:

答案 0 :(得分:1)

我找到了答案。问题是HttpPost链接。该网站使用的是像这样的自定义链接:http://www.online-bustickets.de/bus-berlin-behringersmuehle/

因此每次都必须将字段von和nach添加到链接中。这是工作代码:

private class CustomTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... param) {
        // Do some work
        Log.i("Test", "Working");

        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.editText_von);
        String von = textView.getText().toString();
        AutoCompleteTextView textView1 = (AutoCompleteTextView) findViewById(R.id.editText_nach);
        String nach = textView1.getText().toString();
        EditText datumtext = (EditText) findViewById(R.id.editText_datum);
        String datum = datumtext.getText().toString();

        HttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(
                "http://www.online-bustickets.de/bus-" + von + "-" + nach);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("_searchbox",
                "5624dd9868"));

        nameValuePairs.add(new BasicNameValuePair("von", von));
        nameValuePairs.add(new BasicNameValuePair("nach", nach));
        nameValuePairs.add(new BasicNameValuePair("datum", datum));
        // etc...
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httpPost);

            InputStream is = response.getEntity().getContent();
            Log.i("Test", "response status"
                    + response.getStatusLine().getStatusCode());

            InputStreamReader inputStreamReader = new InputStreamReader(is);

            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);

            StringBuilder stringBuilder = new StringBuilder();

            String bufferedStrChunk = null;

            while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
                stringBuilder.append(bufferedStrChunk);
            }

            content = stringBuilder.toString();

            Log.i("Test", "content is " + content);

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

        return null;
    }

    protected void onPostExecute(Void param) {
        // Print Toast or open dialog
        Intent intent = new Intent(getBaseContext(), WebViewActivity.class);
        intent.putExtra("SITE_CONTENT", content);
        startActivity(intent);
    }
}