“http://54.251.60.177/StudentWebService/StudentDetail.asmx”
上面的链接是Web服务,它包含两个方法,即
对于这两种方法,此方法的输入值为“carrier”
我试图通过SOAP方法从android使用这个Web服务。实际上第二个方法服务以XML的形式返回值,这里我试图使用此方法并尝试将返回的值显示到List -view.But在运行我的模拟器之后,它只是显示一个黑屏。
如何实现我的要求?任何人都能说清楚。
请找我的参考资料
web method.java
package 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));
}
}
提前致谢!..
答案 0 :(得分:1)
行后:
// selecting single ListView item
ListView lv = getListView();
如果与lv相关的适配器是适配器(上面几行),我会这样说,虽然它适用于ArrayAdapter而不是ListAdapter:
lv.setAdapter(adapter);
然后,您必须使用adapter.add(您添加的内容,根据文档我没有错,这是一个字符串)添加元素,并且当添加所有内容时添加:
adapter.notifyDataSetChanged();
最后一个是让元素显示在屏幕上