我正在开发一款安卓应用程序,能够从Lotus Domino数据库中读取数据。我开始创建一个页面来测试HTTP身份验证,但是遇到了许多困难。这是我的代码片段:
public void GoAuth(View v){
final String httpsURL = "http://xxx.xxx.xxx.xxx/names.nsf/mypage?openpage";
final DefaultHttpClient client = new DefaultHttpClient();
final HttpPost httppost = new HttpPost(httpsURL);
String userName = "demo";
String password = "demo";
try {
//authentication block:
final List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("Username", userName));
nvps.add(new BasicNameValuePair("Password", password));
final UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
httppost.setEntity(p_entity);
//sending the request and retrieving the response:
HttpResponse response = client.execute(httppost);
HttpEntity responseEntity = response.getEntity();
if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK){
//handling the response
final InputSource inputSource = new InputSource(responseEntity.getContent());
TextView res=(TextView)findViewById(R.id.result);
res.setText("Server response: "+inputSource.toString());
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
服务器的响应是: org.xml.sax InputSource@40575700
在浏览器中尝试同样的操作,我能够看到登录页面和“我的页面”的内容。 对于在Android上正确的方法和机制,我有些困惑。 非常感谢您的帮助!