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 に答える