Android的HTTP通信 XML解析和异步消息处理
2010-02-03 19:10:00 来源:WEB开发网for (Map.Entry
postData.add(new BasicNameValuePair(m.getKey(), m.getValue()));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postData, HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
String result = "http status code: " + response.getStatusLine().getStatusCode() + " ";
// HttpURLConnection.HTTP_OK
HttpEntity httpEntity = response.getEntity();
InputStream is = httpEntity.getContent();
result += convertStreamToString(is);
textView.setText(result);
} catch (Exception e) {
textView.setText(e.toString());
}
}
// 以 DOM 方式解析 XML(xml 数据详见 res/raw/employee.xml)
private void DOMDemo(){
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(this.getResources().openRawResource(R.raw.employee));
Element rootElement = doc.getDocumentElement();
NodeList employeeNodeList = rootElement.getElementsByTagName("employee");
textView.setText("DOMDemo" + " ");
String title = rootElement.getElementsByTagName("title").item(0).getFirstChild().getNodeValue();
textView.append(title);
for (int i=0; i
Element employeeElement = ((Element)employeeNodeList.item(i));
String name = employeeElement.getAttribute("name");
String salary = employeeElement.getElementsByTagName("salary").item(0).getFirstChild().getNodeValue();
String dateOfBirth =
更多精彩
赞助商链接