地址:http://www.runoob.com/w3cnote/android-tutorial-xml.html
xml解析document另一种:
注意 post解析接口 必须得用子线程,否则失败,同时
得设置上网权限
<!-- 3G网络 -->
<!-- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> -->
<!-- WIFI网络
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
-->
<!-- 网络访问 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- <uses-permission android:name="android.permission.CAMERA" />-->
----------------------------------------
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
if(v.getId()==btnLogin.getId()){
new Thread(new Runnable() {
@Override
public void run() {
// TODO 自动生成的方法存根
String url="-----";
String qiyeID=tb_qiyeID.getText().toString();
String baobaohao=tb_baobaohao.getText().toString();
String mima=tb_mima.getText().toString();
if(TextUtils.isEmpty(qiyeID)||TextUtils.isEmpty(baobaohao)||TextUtils.isEmpty(mima)){
Toast.makeText(MainActivity.this, "请填写完整信息", 2000).show();
return;
}
String xml=MyTool.GetXmlString(qiyeID, baobaohao, mima);
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("xml",xml));
String result=MyTool.post(url, params);
sendMessage("result:"+result);
String jsonString=MyTool.GetJsonString(result);
sendMessage("jsonString:"+jsonString);
String jsonValue=MyTool.DoJsonString(jsonString);
sendMessage("jsonValue:"+jsonValue);
//Toast.makeText(MainActivity.this, result, 3000).show();
}
}).start();
}
}
Handler Myhandle=new Handler() {
public void handleMessage(android.os.Message msg) {
// switch(msg.what){
// case :
// break;
// }
if(msg.obj!=null){
String value=String.valueOf(msg.obj);
Toast.makeText(getApplicationContext(), value, 3000).show();
}
};
};
private void sendMessage(String message){
Message msg=new Message();
msg.obj=message;
Myhandle.sendMessage(msg);
}
--------------------------------------------
public class MyTool {
public MyTool() {
}
public static String GetXmlString(String Customerid, String username,
String password) {
String xml = "";
xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n ";
xml += "<Procedure>\n";
xml += String.format("<username>%s</username>\n", username);
xml += String.format("<password>%s</password>\n", password);
xml += "</Procedure>";
return xml;
}
public static String post(String destUrl, List<NameValuePair> params) {
String strResult = "";
HttpPost httpRequest = new HttpPost(destUrl);//HttpEntityEnclosingRequestBase
// List<NameValuePair> params=new ArrayList<NameValuePair>();
// params.add(new
// BasicNameValuePair("param","I have posted you the data"));
try {
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
strResult = EntityUtils.toString(httpResponse.getEntity());
} else {
System.out.println("Error Response"
+ httpResponse.getStatusLine().toString());
}
} catch (Exception e) {
System.out.println("error occurs"+e.toString());
}
return strResult;
}
public static String GetJsonString(String xml){
String jsonString="";
try {
//①获得DOM解析器的工厂示例:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
//②从Dom工厂中获得dom解析器
DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
//③把要解析的xml文件读入Dom解析器
InputStream in=new ByteArrayInputStream(xml.getBytes());
Document doc = dbBuilder.parse(in);
System.out.println("处理该文档的DomImplemention对象=" + doc.getImplementation());
//④得到文档中名称为person的元素的结点列表
Element el=doc.getDocumentElement();//("string")
jsonString=el.getNodeName();
jsonString=el.getFirstChild().getNodeValue();
}catch(Exception e){
System.out.println("GetJsonString() 错误"+e.toString());
}
return jsonString;
}
public static String DoJsonString(String jsonString){
String value="";
try{
JSONArray jsonArray = new JSONArray(jsonString);
for(int i = 0;i < jsonArray.length();i++){
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
keyNmae="password";
value+=keyNmae+":"+jsonObject.getString(keyNmae)+"\n";
keyNmae="username";
value+=keyNmae+":"+jsonObject.getString(keyNmae)+"\n";
}
}catch (Exception e){
e.printStackTrace();}
return value;
}