从Web服务接收数据时出错并使用AsyncTask将它们绑定到Spinner

时间:2012-11-27 20:13:57

标签: android asp.net web-services ksoap2

我尝试从Web服务方法接收数据返回list (of string)我在ASP.NET中创建此类

<WebMethod()> _
Public Function HelloWorld() As List(Of String)
    Dim names As New List(Of String)
    names.Add("aaa")
    names.Add("bbb")
    names.Add("ccc")
    Return names
End Function

像这样调用结果

<string>aaa</string>
<string>bbb</string>
<string>ccc</string>

现在我需要接收此数据并将其放入Spinner

我做......

public class MainActivity extends Activity 
{

private Spinner myspinner;
private static final String METHOD_NAME = "HelloWorld";  
private static final String NAMESPACE = "http://tempuri.org";   
private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private static final String URL = "http://www.xxxxxxxxxxx.com/ws/AndroidMethods.asmx";
private String[] Values;

private void SektorDoldur()
{
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;     
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.debug = true;
try 
    {
       androidHttpTransport.call(SOAP_ACTION, envelope);
       SoapObject response = (SoapObject) envelope.getResponse();
       Values = new String[response.getPropertyCount()];
       for(int i=0;i<response.getPropertyCount();i++)
          {     
           Values[i] = response.getPropertyAsString(i).toString();
          }      
    }
catch (Exception e)
    {
    e.printStackTrace(); 
    }
}

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myspinner = (Spinner)findViewById(R.id.spinner1);
    new MyTask().execute();
}
public class MyTask extends AsyncTask<Void, Void, String>
{

String response = "";
public void onPreExecute() 
  {
    super.onPreExecute();
  }
    @Override
protected String doInBackground(Void... arg0)
{
       SektorDoldur();
       return "";
}
@Override
public void onPostExecute(String res)
{

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, Values);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    myspinner.setAdapter(adapter);           
}
}
}

此行

出错
ArrayAdapter<String> adapter = new ArrayAdapter<String>this,android.R.layout.simple_spinner_item, Values);
onPostExecute方法

中的

你能帮助我做到吗......

1 个答案:

答案 0 :(得分:2)

  

构造函数ArrayAdapter(MainActivity.MyTask,int,String [])未定义

在这一行:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Values);

this指的是MyTask类,而不是您期望的Activity的Context。您需要传递对有效Context的引用,例如:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, Values);

添加

  

我如何从二维中检索列表,如

Public Class NameAndvalue
    Public name As String
    Public value As String
End Class
     

我需要在微调器中显示名称,并在点击某个项目时显示值...

一种方法是使用List<Map<String, String>> values将每个namevalue存储在地图中,并使用SimpleAdapter在Spinner中显示name字符串:

SimpleAdapter adapter = new SimpleAdapter(this, values, 
        android.R.layout.simple_spinner_item, 
        new String[] {"name"}, new int[] {android.R.id.text});

最后,在onItemSelected()方法中,您现在可以使用value值而不是name值。