2012-08-30 29 views
0

“http://54.251.60.177/StudentWebService/StudentDetail.asmx”无法显示在Android /在列表视图值蚀

上述链路是web服务,其包含两个方法,即

  1. GetStudentDetails
  2. GetStudentsDetailsXML

该方法的输入值是“载体”对于两种方法

我想通过SOAP方法从android中使用此web服务。实际上,第二个方法服务正在返回XML形式的值,在这里我试图使用此方法并试图显示返回的值List-view.But在运行我的模拟器之后,它只是简单地显示一个黑屏。

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

请找我参考

网络method.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)); 
    } 
} 

在此先感谢..

回答

1

行后:

// selecting single ListView item 

ListView lv = getListView(); 

如果LV的相关适配器适配器(上面有几行),我会把这种方式,虽然它与工作ArrayAdapter而不是ListAdapter:

lv.setAdapter(adapter); 

然后,你必须添加元素与adapter.add适配器(你加什么,这是一个字符串,如果我根据文档我没看错),当所有被添加广告d:

adapter.notifyDataSetChanged(); 

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