有一个网站,其中有几个下拉框.我制作了一个Android应用程序,用于从网站中提取值.现在网站上有一个搜索框,在网站上,我们可以从框中选择选项,然后按Submit,然后根据所选的选项给出结果.我需要在我的应用中执行相同的操作.
需要帮助.谢谢
解决方法:
要将数据发布到网站,您已经向其发送了HTTP POST请求.您可以将要发送的数据放入数组,然后将其发送到PHP脚本.
您必须弄清楚您的String是通过哪个ID发送到服务器的.在我的示例中,它是your_1和your_2.每个网站都不同.所有新的浏览器都可以在开发人员控制台中读取内容.
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.PHP");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("your_1", "data 1"));
nameValuePairs.add(new BasicNameValuePair("your_2", "data 2"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// Todo Auto-generated catch block
} catch (IOException e) {
// Todo Auto-generated catch block
}
发送完之后,您必须获得响应,您可以使用StringBuilder读出该响应.
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
while ((line = rd.readLine()) != null) {
total.append(line);
}
// Return full string
return total;
}