从Android应用程序登录网页

时间:2013-08-19 20:01:26

标签: android http login

我是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;
}

1 个答案:

答案 0 :(得分:0)

您要求的是数据抓取。您必须解析返回HTML以获得您的价值。您可以制作自己合适的HTML解析器,但我建议使用这样的库。