0

"http://54.251.60.177/StudentWebService/StudentDetail.asmx"

上記のリンクは、2 つのメソッドを含む Web サービスです。

  1. GetStudentDetails
  2. GetStudentsDetailsXML

このメソッドの入力値は 、両方のメソッドの「キャリア」です

メソッドSOAPでAndroidからこのWebサービスを消費しようとしています。実際には、2番目のメソッドサービスは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 に関連するアダプターがアダプター (上記の数行) である場合、ListAdapter ではなく ArrayAdapter で機能しますが、次のようになります。

lv.setAdapter(adapter);

次に、adapter.add(追加したもの、ドキュメントに従って間違っていない場合は文字列)を使用してアダプターに要素を追加する必要があり、すべてが追加されたら次を追加します。

adapter.notifyDataSetChanged();

最後に、画面に表示される要素を作成します。

于 2012-08-30T15:04:36.343 に答える