0

“http://54.251.60.177/StudentWebService/StudentDetail.asmx”

上面的链接是web服务,它包含两个方法,即

  1. 获取学生详细信息
  2. GetStudentsDetailsXML

此方法的输入值是 两种方法的“载体”

我正在尝试通过 SOAP 方法从 android 使用此 Web 服务。实际上,第二种方法服务以 XML 的形式返回值,这里我尝试使用此方法并尝试将返回的值显示到列表视图中。但是在运行我的模拟器后,它只是显示黑屏。

如何达到我的要求?任何人都可以让我清楚。

请找到我的参考资料

网络方法.java

包 org.test.web.services;

public class GetStudentsDetailsXML 
{
public String GetStudentsDetailsXML(String fieldName)
{
    return fieldName;
}
}

AndroidXMLParsingActivity.java

 public class AndroidXMLParsingActivity extends ListActivity 
  {

private String METHOD_NAME = "GetStudentsDetailsXML";
private String NAMESPACE = "http://tempuri.org/";
private String SOAP_ACTION ="http://tempuri.org/GetStudentsDetailsXML";
private static final String URL = "http://54.251.60.177/StudentWebService/StudentDetail.asmx?WSDL";

EditText edt1,edt2;
Button btn;
TextView tv;

static final String URL_XML = "http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetStudentsDetailsXML";

//XML node keys

static final String KEY_TABLE = "Table"; // parent node
static final String KEY_FIELDTYPE = "FieldType";
static final String KEY_FIELDFORMAT = "FieldFormat";
static final String KEY_SAMPLE = "Sample";
static final String KEY_SEARCH = "SearchTags";

@Override
public void onCreate(Bundle savedInstanceState) 
{


    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btn = (Button)findViewById(R.id.button_get_result);
    edt1 = (EditText)findViewById(R.id.editText1);

    btn.setOnClickListener(new View.OnClickListener()
    {
     public void onClick(View v)
     {

     //Initialize soap request + add parameters

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);       

        //Use this to add parameters
        request.addProperty("fieldName",edt1.getText().toString());

        //Declare the version of the SOAP request
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;

        try 
        {

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        //this is the actual part that will call the webservice

        androidHttpTransport.call(SOAP_ACTION, envelope);

        SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

        System.out.println("Result : "+ result.toString());
     }

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

    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();

    String xml = parser.getXmlFromUrl(URL_XML); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_TABLE);

    // looping through all item nodes <item>

    for (int i = 0; i < nl.getLength(); i++) 
    {

        // creating new HashMap

    HashMap<String, String> map = new HashMap<String, String>();
    Element e = (Element) nl.item(i);

    // adding each child node to HashMap key => value

    map.put(KEY_FIELDTYPE,   parser.getValue(e, KEY_FIELDTYPE));
    map.put(KEY_FIELDFORMAT, parser.getValue(e, KEY_FIELDFORMAT));
    map.put(KEY_SAMPLE, parser.getValue(e, KEY_SAMPLE));
    map.put(KEY_SEARCH, parser.getValue(e, KEY_SEARCH));

    // adding HashList to ArrayList

    menuItems.add(map);
    }

    // Adding menuItems to ListView

    ListAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.list_item,new String[] { KEY_FIELDFORMAT, KEY_SEARCH, KEY_SAMPLE }, new int[] 
            {R.id.FieldFORMAT_textView, R.id.Search_TEXTVIEW, R.id.Sample_TEXTVIEW });


    setListAdapter(adapter);

    // selecting single ListView item

    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) 
        {
            // getting values from selected ListItem

            String FieldFormat = ((TextView) view.findViewById(R.id.FieldFORMAT_textView)).getText().toString();
            String Sample = ((TextView) view.findViewById(R.id.Sample_TEXTVIEW)).getText().toString();
            String Search = ((TextView) view.findViewById(R.id.Search_TEXTVIEW)).getText().toString();

            // Starting new intent

            Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
            in.putExtra(KEY_FIELDFORMAT, FieldFormat);
            in.putExtra(KEY_SAMPLE, Sample);
            in.putExtra(KEY_SEARCH, Search);
            startActivity(in);

        }
    }); } }

SingleMenuItemActivity.java

public class SingleMenuItemActivity  extends Activity 
{

// XML node keys

static final String KEY_FIELDFORMAT = "FieldFormat";
static final String KEY_SAMPLE = "Sample";
static final String KEY_SEARCH = "SearchTags";

@Override

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_list_item);

    // getting intent data
    Intent in = getIntent();

    // Get XML values from previous intent
    String Fieldformat = in.getStringExtra(KEY_FIELDFORMAT);
    String Sample = in.getStringExtra(KEY_SAMPLE);
    String Search = in.getStringExtra(KEY_SEARCH);

    // Displaying all values on the screen

    TextView lblName = (TextView) findViewById(R.id.fieldformat_label);
    TextView lblCost = (TextView) findViewById(R.id.Sample_label);
    TextView lblDesc = (TextView) findViewById(R.id.Search_label);

    lblName.setText(Fieldformat);
    lblCost.setText(Sample);
    lblDesc.setText(Search);
}}

XMLParser.java

 public class XMLParser 
 {

// constructor
public XMLParser() 
{

}

/**
 * Getting XML from URL making HTTP request
 * @param url string
 * */
public String getXmlFromUrl(String urlxml) {
    String xml = null;

    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(urlxml);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
}

/**
 * Getting XML DOM element
 * @param XML string
 * */
public Document getDomElement(String xml){
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is); 

        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }

        return doc;
}

/** Getting node value
  * @param elem element
  */
 public final String getElementValue( Node elem ) {
     Node child;
     if( elem != null){
         if (elem.hasChildNodes()){
             for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                 if( child.getNodeType() == Node.TEXT_NODE  ){
                     return child.getNodeValue();
                 }
             }
         }
     }
     return "";
 }

 /**
  * Getting node value
  * @param Element node
  * @param key string
  * */
 public String getValue(Element item, String str) {     
        NodeList n = item.getElementsByTagName(str);        
        return this.getElementValue(n.item(0));
    }
 }

提前致谢!..

4

1 回答 1

1

行后:

// selecting single ListView item

ListView lv = getListView();

如果与 lv 相关的适配器是适配器(上面几行),我会这样说,尽管它适用于 ArrayAdapter 而不是 ListAdapter:

lv.setAdapter(adapter);

然后,您必须使用 adapter.add 将元素添加到适配器(您添加的内容,如果根据文档我没记错的话,这是一个字符串),当全部添加时添加:

adapter.notifyDataSetChanged();

最后一个是使元素显示在屏幕上

于 2012-08-30T15:04:36.343 回答