真心伤不伏,原来Android中不能解析以127.0.0.1和localhost开头的XML文件
我部署在本地的WEBSERVICE地址如图所示:
----------------------------------------------------------------
MainActivity的代码如下所示:
package cn.yljd.db;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.ClientProtocolException;
import cn.yljd.service.MyXMLParser;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity {
private EditText userText;
private EditText pwdText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
userText=(EditText) this.findViewById(R.id.edit1);
pwdText=(EditText) this.findViewById(R.id.edit2);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new buttonOnClickListener());
}
// private final class buttonOnClickListener implements View.OnClickListener{
// public void onClick(View v) {
// String username = userText.getText().toString();
// String password = pwdText.getText().toString();
// MyXMLParser parser=new MyXMLParser();
// List<String[]> list = parser.xml2list("http://www.me.com:2946/Service1.asmx/select", "diffgr:diffgram/NewDataSet/Table", "User_Id,User_Name,User_Password");
// for (String[] str:list)
// {
//
// System.out.println("str[1]="+str[0]);
// System.out.println("str[2]="+str[1]);
// //System.out.println("1");
// //if((username.equals("1") && password.equals("2"))){
// //Toast.makeText(getApplicationContext(),"登陆成功or失败",1).show();
// //}
// //else
// if (username.equals(str[1]) && password.equals(str[2])){
// Toast.makeText(getApplicationContext(),"登陆成功",Toast.LENGTH_SHORT).show();
// break;
// }
// //else{
// //Toast.makeText(getApplicationContext(),"登陆失败",0).show();
// //}
// }
// Toast.makeText(getApplicationContext(),"登陆失败",Toast.LENGTH_SHORT).show();
// }
// }
private final class buttonOnClickListener implements View.OnClickListener{
public void onClick(View v) {
String username = userText.getText().toString();
String password = pwdText.getText().toString();
MyXMLParser parser=new MyXMLParser();
List<String[]> list =(parser.xml2list("http://192.168.66.175/Service1.asmx/select", "diffgr:diffgram/NewDataSet/Table", "User_Id,User_Name,User_Password"));
for(String[] str:list){
//System.out.println(str[0]);
if((username.equals(str[1])&&password.equals(str[2]))){
System.out.println("successful");
Toast.makeText(getApplicationContext(),"登陆成功",1).show();
break;
}else {
Toast.makeText(getApplicationContext(),"登陆失败",0).show();
System.out.println("fail");
break;
}
}
}
}
}
---------------------------------------------------------
MyXMLParser类代码如下所示:
package cn.yljd.service;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.*;
import org.xml.sax.*;
public class MyXMLParser {
public static List<String[]> xml2list(String url, String xpath, String fields)
{
List<String[]> mList = new ArrayList<String[]>();
String[] path = xpath.split("/");
String[] field = fields.split(",");
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
Document document = null;
factory = DocumentBuilderFactory.newInstance();
try {
String content = getXML(url);
builder = factory.newDocumentBuilder();
document = builder.parse(new ByteArrayInputStream(content.getBytes("UTF8")));
Element root = document.getDocumentElement();
Element tmp = (Element) root.getElementsByTagName(path[0]).item(0);
for (int i=1;i<path.length-1;i++)
{
tmp = (Element)tmp.getElementsByTagName(path[i]).item(0);
}
NodeList nodes = tmp.getElementsByTagName(path[path.length-1]);
for (int i=0;i<nodes.getLength();i++)
{
Element item = (Element) nodes.item(i);
String[] tmpString = new String[fields.length()];
for (int j = 0;j<field.length;j++)
{
Element tmpElement = (Element) item.getElementsByTagName(field[j]).item(0);
tmpString[j] = tmpElement.getFirstChild().getNodeValue();
}
mList.add(tmpString);
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mList;
}
public static String getXML(String url) throws ClientProtocolException, IOException
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer sb = new StringBuffer();
String line = null;
while ((line = reader.readLine())!=null)
{
sb.append(line);
}
return sb.toString();
}
}