其他分享
首页 > 其他分享> > 无法将ksoap对象绑定到Android中的ArrayList

无法将ksoap对象绑定到Android中的ArrayList

作者:互联网

我正在开发一个调用Web服务的应用程序,然后webservice返回一个数组列表.我的问题是我无法将数据导入ArrayList,然后在ListView中显示.我有什么想法我做错了吗?我知道Web服务返回一个ArrayList.一切似乎工作正常,ListView或ArrayList中没有数据…..提前感谢!

编辑:我觉得webservice响应的数据是一个复杂的类型.我真的认为这是我被挂断的地方,但不知道如何纠正它.

2010年4月21日:

所以我终于想通了我的webservice返回一个JSON数组.现在将其转移到ArrayList和/或ListView是一个挑战.我有一种感觉,一旦这个项目完成,它可能会成为一个很棒的教程或示例……

package com.maskau;

import java.util.ArrayList;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import android.app.*;
import android.os.*;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;


public class Home extends Activity implements Runnable{

/** Called when the activity is first created. */
public static final String SOAP_ACTION = "http://bb.mcrcog.com/GetArtist";
public static final String METHOD_NAME = "GetArtist";
public static final String NAMESPACE = "http://bb.mcrcog.com/";
public static final String URL = "http://bb.mcrcog.com/karaoke/service.asmx";
String wt;
public static ProgressDialog pd;
TextView text1;
ListView lv;
static EditText myEditText;
static Button but;
private ArrayList<String> Artist_Result = new ArrayList<String>();



@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    myEditText = (EditText)findViewById(R.id.myEditText);
    text1 = (TextView)findViewById(R.id.text1);
    lv = (ListView)findViewById(R.id.lv);
    but = (Button)findViewById(R.id.but);

    but.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            wt = ("Searching for " + myEditText.getText().toString());
            text1.setText("");
            pd = ProgressDialog.show(Home.this, "Working...", wt , true, false);
            Thread thread = new Thread(Home.this);
            thread.start();
        }
    }
    );
}

public void run()
{
        try
     {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            PropertyInfo pi = new PropertyInfo();
            pi.setName("ArtistQuery");
            pi.setValue(Home.myEditText.getText().toString());
            request.addProperty(pi);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);

            AndroidHttpTransport at = new AndroidHttpTransport(URL);
            at.call(SOAP_ACTION, envelope);
            java.util.Vector<Object> rs = (java.util.Vector<Object>)envelope.getResponse();
            if (rs != null)
            {
                for (Object cs : rs)
                {
                    Artist_Result.add(cs.toString());
                }
            }
        }

     catch (Exception e)
        {
         // Added this line, throws "org.ksoap2.serialization.SoapObject" when run
            Artist_Result.add(e.getMessage());      
      }

     handler.sendEmptyMessage(0);
}
private Handler handler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        ArrayAdapter<String> aa;
    aa = new ArrayAdapter<String>(Home.this, android.R.layout.simple_list_item_1, Artist_Result);
    lv.setAdapter(aa);
    try
    {
        if (Artist_Result.isEmpty())
        {
               text1.setText("No Results");
        }
        else
        {

               text1.setText("Complete");
               myEditText.setText("Search Artist");
        }
    }
    catch(Exception e)
    {
    text1.setText(e.getMessage());
    }
    aa.notifyDataSetChanged();
    pd.dismiss();
    }
};
}

来自webservice的示例结果数据:

<ArrayOfArtists>
    <Artist>
        <Track>.......</Track>
    </Artist>
    <Artist>
        <Track>.......</Track>
    </Artist>
    <Artist>
        <Track>.......</Track>
    </Artist>
</ArrayOfArtists>

解决方法:

你应该在onCreate和handleMessage中设置适配器,只需调用lv.getAdapter().notifyDataSetChanged()

标签:json,android,arraylist,web-services,ksoap2
来源: https://codeday.me/bug/20190710/1422787.html