2013-03-14 48 views
3

有一个网站,其中有几个下拉框。我做了一个android应用程序,从网站拉的价值。现在网站上有一个搜索框,在网站上我们可以从框中选择选项并按提交,然后根据选择的选项给出结果。我需要在我的应用程序中做同样的事情。 需要帮助。谢谢发送数据到网站-Android

+0

你有什么试图做到这一点.. – 2013-03-14 17:29:33

+2

@TGMCians我通过页面看到的源代码来查看当我们点击网站时该页面重定向到的链接。现在我尝试使用网站中使用的过滤器链接,我没有找到(预期)的链接。现在我想像我们在网页中那样做一个微调框架结构,但为此我们需要数据库元素(就像我们将数据从jsp传递到servlet一样)。但是我仍然需要具体的想法来继续 – SeasonalShot 2013-03-14 17:38:48

回答

4

要发布数据到网站,您必须发送一个HTTP POST请求给它。您可以将要发送的数据放入数组中,并将其发送到php脚本。

你必须弄清楚你的字符串以哪个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; 
} 

现在您已经收到回复,您可以使用RegEx强调您的特殊文本。这有点棘手,但this会帮助你。