0

我是 Android 的新手,今天我遇到了一个问题。我想登录这个网站:

http://pedidos.pizzeriabritannia.com/index.asp?Opc=Pedir

并从其源代码中检索一些数据,但我当然不知道从哪里开始。你能给我一些建议或一些文献来处理这个问题吗?

谢谢小伙伴们!

我有以下代码:

public void postLoginData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();

    /* login.php returns true if username and password is equal to saranga */
    HttpPost httppost = new HttpPost("http://pedidos.pizzeriabritannia.com/index.asp?Opc=Pedir");

    try {
        // Add user name and password


        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", "942037851"));
        nameValuePairs.add(new BasicNameValuePair("password", "xxxxxx"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        Log.w("SENCIDE", "Execute HTTP Post Request");
        HttpResponse response = httpclient.execute(httppost);

        String str = inputStreamToString(response.getEntity().getContent()).toString();
        Log.w("SENCIDE", str);

        if(str.toString().equalsIgnoreCase("true"))
        {
         Log.w("SENCIDE", "TRUE");
         et1.setText("Login successful");   
        }else
        {
         Log.w("SENCIDE", "FALSE");
         et1.setText("Login not successful");             
        }

    } catch (ClientProtocolException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }
} 

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
 try {
  while ((line = rd.readLine()) != null) { 
    total.append(line); 
  }
 } catch (IOException e) {
  e.printStackTrace();
 }
 // Return full string
 return total;
}
4

1 回答 1

0

您所要求的称为数据抓取。您必须解析返回的 HTML 才能获得您的价值。您可以制作自己合适的 HTML 解析器,但我建议您使用此类库。

于 2014-03-12T07:01:06.513 回答