Android webservice客户端:数据如何从xml文件中提取数据

时间:2013-03-29 13:54:37

标签: java android eclipse web-services webservice-client

我正在创建一个android webservice客户端(restful web service)。我试图从webservice读取一个简单的值,但是在我的应用程序中,我无法从webservice xml文件中提取数据值。

从网络服务返回的数据如下所示:

enter image description here

在我的应用中,数据如下所示:

enter image description here

我只需要读出值而不是整个xml文件。我有什么想法可以做到这一点吗?

这是我的代码:

    MainActivity.java
package com.hit.resttest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.os.AsyncTask;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener 
{

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.my_button).setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) 
    {
     Button b = (Button)findViewById(R.id.my_button);
     b.setClickable(false);
     new LongRunningGetIO().execute();
    }

    private class LongRunningGetIO extends AsyncTask <Void, Void, String> 
    {
     protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException 
     {
      InputStream in = entity.getContent();
      StringBuffer out = new StringBuffer();
      int n = 1;
      while (n>0) {
       byte[] b = new byte[4096];
       n =  in.read(beer);
       if (n>0) out.append(new String(b, 0, n));
     }
     return out.toString();
    }

     @Override
     protected String doInBackground(Void... params) 
     {
      HttpClient httpClient = new DefaultHttpClient();
      HttpContext localContext = new BasicHttpContext();
      HttpGet httpGet = new HttpGet("http://192.168.1.118:8080/WebService/random");
      String text = null;
      try 
      {
       HttpResponse response = httpClient.execute(httpGet, localContext);
       HttpEntity entity = response.getEntity();
       text = getASCIIContentFromEntity(entity);
      } 

      catch (Exception e) 
      {
       return e.getLocalizedMessage();
      }
      return text;
     }

     protected void onPostExecute(String results) 
     {
      if (results!=null) 
      {
       EditText et = (EditText)findViewById(R.id.my_edit);
       et.setText(results);
      }

       Button b = (Button)findViewById(R.id.my_button);
       b.setClickable(true);
      }
     }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }    
}

1 个答案:

答案 0 :(得分:2)

您必须解析xml并将值设置为视图。这是一个例子。

//import 
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

String test = "<Response><Terminal><Name>name</Name><Value>1</Value></Terminal></Response>";
    // parse
    try {

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        InputSource s = new InputSource(new StringReader(test)); 
        Document doc = dBuilder.parse(s);

        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("Terminal");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println("value : " + eElement.getElementsByTagName("Value").item(0).getTextContent());

            }
        }
        } catch (Exception e) {
        e.printStackTrace();
        }