「http://54.251.60.177/StudentWebService/StudentDetail.asmx」無法顯示在Android /在列表視圖值蝕
上述鏈路是web服務,其包含兩個方法,即
- GetStudentDetails
- 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));
}
}
在此先感謝..