您可以使用 HttpPost 和 HttpGet 请求与服务器通信。HttpGet 在 url 中发送参数(例如:http://mysite.com/index.html?username=admin&password=cleartext)。这显然不是安全信息的首选方法。HttpPost 在使用 https 加密的数据包中发送数据。这是将用户输入的数据发送到网页的示例。
EditText usernameText = (EditText)findViewById(R.id.username);
EditText passwordText = (EditText)findViewById(R.id.password);
String postParameters = "u=" + usernameText.getText() + "&p=" + passwordText.getText();
try {
DefaultHttpClient kccClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://www.mywebsite.com/login.php");
HttpEntity postEntity = new StringEntity(postParameters);
postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
postRequest.setEntity(postEntity);
HttpResponse postResponse = kccClient.execute(postRequest);
HttpEntity postResponseEntity = postResponse.getEntity();
responseText.setText(EntityUtils.toString(postResponseEntity));
} catch(Exception e) {
responseText.setText(e.getMessage());
}
要使用安全连接,只需将网址更改为https://www.mywebsite.com/login.php。