Reputation: 573
I made an app that receives 3 text fields and it needs to send the data to a website. I have looked at the php code of the main page and since I'm not that good with php, I'm not sure how exactly to post the data. Here is how the form looks on the site:
<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>
My app need to submit this form using the values entered in the text fields. The problem I have is that it doesn't just use a script.php file when it submits, it uses a function from a class. Here's the code from 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>
And here is the code for the function 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
}
So, how could I post the data to the website? I need it to return a webpage that will then be displayed in a WebView.
UPDATE My code looks like this now:
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();
}
But when I try to open it in a webview I get a page saying that the value for von and nach are not in the database. Does anyone have any ideas what the problem might be?
UPDATE1 I get the error message that you get on the site when you do a search without entering any values. This is the link with the error: http://www.online-bustickets.de/bus--/
Upvotes: 0
Views: 984
Reputation: 573
I found the answer. The problem was the HttpPost link. The website was using a custom link like this one: http://www.online-bustickets.de/bus-berlin-behringersmuehle/
So the fields von and nach must be added to the link every time. This is the working code:
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);
}
}
Upvotes: 1