使用从asp.net WebService检索的数据填充Android Spinner

时间:2014-02-04 21:20:22

标签: android asp.net web-services spinner populate

所以我实际上是在尝试检索我在asp.net Web服务中的数据并将其传递给android spinner。由于我只接收强制关闭错误或空值,因此我很难完成它。

我将这些网站用于我的代码,site1site2 所以这是我的WebService:

[WebMethod]
    public ArrayList GetCustomerList()
    {
        ArrayList list = new ArrayList();
        SqlConnection con = DB.GetConnection();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT * FROM Regions";
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            list.Add(reader["regName"].ToString());
        }
        con.Close();
        list.ToArray();
        return list;
    }

这是我的android代码,它只给我一个空值: 主要活动;

package com.example.lainnasad;

import java.io.IOException;
import java.util.Arrays;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

String[] regionList;
String regions;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new region().execute();
}

class region extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... arg0) {
        final String SOAP_ACTION = "http://tempuri.org/GetCustomerList";
        final String METHOD_NAME = "GetCustomerList";
        final String URL = "http://xxx.xxx.x.xxx:xxxx/WebPopulate.asmx";
        final String NAMESPACE = "http://tempuri.org/";

        SoapObject Request = new SoapObject(NAMESPACE,METHOD_NAME);
        Bean C = new Bean();
        PropertyInfo pi = new PropertyInfo();
        pi.setName("Bean");
        pi.setValue(C);
        pi.setType(C.getClass());
        Request.addProperty(pi);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;

        envelope.setOutputSoapObject(Request);
        envelope.addMapping(NAMESPACE, "Bean", new Bean().getClass());
        HttpTransportSE aht = new HttpTransportSE(URL);
        aht.debug = true;

        try {
            System.out.println("Request:"+Request);
            aht.call(SOAP_ACTION, envelope);
            SoapObject response = (SoapObject) envelope.getResponse();
            System.out.println("MainActivity.onCreate()"+response);
            envelope.addMapping(NAMESPACE, "Bean",new Bean().getClass());
            aht.call(SOAP_ACTION, envelope);
            Bean[] countrylist = new Bean[response.getPropertyCount()];

            for (int j = 0; j < countrylist.length; j++) {
            SoapObject pii = (SoapObject) response.getProperty(j);
            Bean beanobject = new Bean();
            beanobject.Region = pii.getProperty(0).toString();
            countrylist[j] = beanobject;
            System.out.println("person: " + beanobject.Region);
            }

            int i = 0;
            regionList = new String[countrylist.length];
            for (i = 0; i < countrylist.length; i++) {
            regionList[i] = Arrays.asList(countrylist[i].Region).toString().replace("[", "").replace("]", "");
            System.out.println("stringarray: " + regionList);
            }
            }
            catch (IOException e) {
            e.printStackTrace();
            } catch (XmlPullParserException e) {
            e.printStackTrace();
            }
            catch (Exception e) {
            e.printStackTrace();
            }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        spinner1meth();
    }


}

public void spinner1meth(){

    Spinner spinner = (Spinner) findViewById(R.id.spinner1);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,regionList);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这是Bean类:

package com.example.lainnasad;

import java.util.Hashtable;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class Bean implements KvmSerializable {

public String Region;

public Bean(){}

public Bean(String Region,String State) {
this.Region= Region;
//this.State=State;
}

@Override
public Object getProperty(int arg0) {
switch(arg0)
{
case 0:
return Region;
}
return null;
}

@Override
public int getPropertyCount() {
return 1;
}
@Override

public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
switch(index)
{
case 0:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Country";
break;
default:
break;
}
}

@Override
public void setProperty(int index, Object value) {
switch(index)
{
case 0:
Region= value.toString();
break;
default:
break;
}
}

}
你能帮我解决一下缺少的东西吗?提前谢谢!

以下是log-cat的内容。我觉得很困惑。对不起。刚开始安卓。

02-06 07:01:07.862: E/AndroidRuntime(1449): FATAL EXCEPTION: main
02-06 07:01:07.862: E/AndroidRuntime(1449): Process: com.example.lainnasad, PID: 1449
02-06 07:01:07.862: E/AndroidRuntime(1449): java.lang.NullPointerException: storage == null
02-06 07:01:07.862: E/AndroidRuntime(1449):     at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at java.util.Arrays.asList(Arrays.java:155)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at   com.example.lainnasad.MainActivity.spinner1meth(MainActivity.java:104)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at com.example.lainnasad.MainActivity$region.onPostExecute(MainActivity.java:94)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at com.example.lainnasad.MainActivity$region.onPostExecute(MainActivity.java:1)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at android.os.AsyncTask.finish(AsyncTask.java:632)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at android.os.Looper.loop(Looper.java:137)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at android.app.ActivityThread.main(ActivityThread.java:4998)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at java.lang.reflect.Method.invokeNative(Native Method)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at java.lang.reflect.Method.invoke(Method.java:515)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-06 07:01:07.862: E/AndroidRuntime(1449):     at dalvik.system.NativeStart.main(Native Method)

我已将我发布的其他代码更改为此代码。仍然遇到这些错误。

0 个答案:

没有答案