1

我是 Android 开发的新手。我制作了一个用于登录网站的 Android 应用程序。登录页面需要三个输入“用户名”、“密码”和“密码”。我已经使用 HttpPost 方法成功地传递了这些数据。看代码,

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>
4

1 回答 1

0

按照@tobias 的建议,用于response.getEntity().getContent()获取输入流并使用 DOM、SAX 或 XPath 对其进行处理。有很多选择。

您也可以直接获取 xml 字符串。换行

HttpResponse response = httpclient.execute(httppost);

String xml = httpclient.execute(httppost, new BasicResponseHandler());
于 2013-06-20T17:21:52.957 回答