WEB开发网
开发学院手机开发Android 开发 Android的HTTP通信 XML解析和异步消息处理 阅读

Android的HTTP通信 XML解析和异步消息处理

 2010-02-03 19:10:00 来源:WEB开发网   
核心提示:ArrayList();for (Map.Entry m : data.entrySet()) {postData.add(new BasicNameValuePair(m.getKey(), m.getValue()));}UrlEncodedFormEntity entity = new UrlEncodedFor
ArrayList();

for (Map.Entry m : data.entrySet()) {

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 =

上一页  1 2 3 4 5 6 7 8 9 10  下一页

Tags:Android HTTP 通信

编辑录入:coldstar [复制链接] [打 印]
赞助商链接