0

我正在做一个HttpPost请求作为回报 我得到一个 XML 我可以把它吐司 我在一个字符串中

try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://xx.xx.xx.xx/login.asmx/login");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        String responseBody = EntityUtils.toString(response.getEntity());
        Toast.makeText( getApplicationContext(),"responseBody:   "+responseBody,Toast.LENGTH_SHORT).show();

XML看起来像

<root status="Y">

<mb mbcode="150201" mbname="AKASH KUNDU" branchid="1" pwd="admin"/>

</root>

我想解析这个 XML 并将这个数据存储为一个全局变量。我没有找到任何虔诚的例子,这就是为什么我冒充我是新来的提前谢谢。

4

2 回答 2

1

我找到了我的解决方案

HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://xx.xx.xx.xx/login.asmx/login");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        String responseBody = EntityUtils.toString(response.getEntity());


        //saving the file as a xml
        FileOutputStream fOut = openFileOutput("loginData.xml",MODE_WORLD_READABLE);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.write(responseBody);
        osw.flush();
        osw.close();

        //reading the file as xml
        FileInputStream fIn = openFileInput("loginData.xml");
        InputStreamReader isr = new InputStreamReader(fIn);
        char[] inputBuffer = new char[responseBody.length()];
        isr.read(inputBuffer);
        String readString = new String(inputBuffer);



        //getting the xml Value as per child node form the saved xml
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        //InputStream is = new ByteArrayInputStream(responseBody.getBytes("UTF-8"));
        InputStream is = new ByteArrayInputStream(readString.getBytes("UTF-8"));
        Document doc = db.parse(is);

        /*DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc=builder.parse(fIn, null);*/
        NodeList root=doc.getElementsByTagName("root");




        for (int i=0;i<root.getLength();i++) {
            loginStatus = "" + ((Element)root.item(i)).getAttribute("status");
        }


            if(loginStatus.equalsIgnoreCase("Y"))
            {
                NodeList mb=doc.getElementsByTagName("mb");
                for (int i=0;i<mb.getLength();i++) {
                    setMbcode("" + ((Element)mb.item(i)).getAttribute("mbcode"));
                    setMbname("" + ((Element)mb.item(i)).getAttribute("mbname"));
                    branchid  = "" + ((Element)mb.item(i)).getAttribute("branchid");
                    pwd       = "" + ((Element)mb.item(i)).getAttribute("pwd");

                }
于 2015-08-18T10:38:36.153 回答
0

试试 Android 开发者指南,或http://developer.android.com/training/basics/network-ops/xml.html使用google也不是太难。

于 2013-02-03T11:44:34.620 回答