1
我是Android開發新手。我製作了一個Android應用程序,用於在網站上登錄。登錄頁面需要三個輸入'用戶名','密碼'&'pin'。 我已經使用HttpPost方法成功地傳遞了這些數據。看到代碼,如何從Android中的HttpResponse解析這個特定的XML?
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.mysite.com/api/service.php");
try {
// Add user name, password & pin
String action = "login";
EditText uname = (EditText)findViewById(R.id.txt_username);
String username = uname.getText().toString();
EditText pword = (EditText)findViewById(R.id.txt_password);
String password = pword.getText().toString();
EditText pcode = (EditText) findViewById (R.id.txt_pin);
String pin = pcode.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("action", action));
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
nameValuePairs.add(new BasicNameValuePair("pin", pin));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
Log.w("PS", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
現在我想從解析的的HttpResponse 'ack'
& 'msg'
這是將數據傳遞到網站後,XML輸出顯示。看到這裏,
<login>
<member>
<id/>
<username/>
<name/>
<ewallpoints/>
<ack>FAILED</ack>
<msg>Wrong Username and Password</msg>
</member>
</login>
Java提供DOM或SAX解析器,例如http://javarevisited.blogspot.de/2011/12/parse-xml-file-in-java-example-tutorial.html – tobias
感謝您的回覆,我檢查了鏈接非常有用,但它在離線時解析XML模式,你能告訴我如何我可以直接從http響應解析XML? –