如何从Webservice中检索字符串数组并在android中的toast中显示



我想从我的webservice中检索字符串数组,并必须在Toast中的android项目中显示它。我这样编码它,但它没有显示任何内容。有人能帮我吗?(

public class InsertData extends MainActivity {
EditText txt_1,txt_2;
Button SetData,GetData;
private static String NAMESPACE = "http://tempuri.org/";
private static String URL = "http://10.0.2.2:49371/WebService1.asmx";
private static String SOAP_ACTION = "http://tempuri.org/GetData";
private static String SOAP_ACTION2 = "http://tempuri.org/ThrowData";
private static String METHOD_NAME = "GetData";
private static String METHOD_NAME2 = "ThrowData";
String DateTime;
String IMEI;
//private static String[] arrString = new String[40];
Long time = (long) 3000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setdata);
    txt_1 = (EditText) findViewById (R.id.editText1);
    txt_2 = (EditText) findViewById (R.id.editText2);
    SetData = (Button) findViewById(R.id.button1);
    GetData = (Button) findViewById(R.id.button2);
    Calendar c = Calendar.getInstance();
    SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy : HH:mm:ss");
    DateTime = df.format(c.getTime());
    txt_1.setText(DateTime);
    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    IMEI = telephonyManager.getDeviceId();
    txt_2.setText(IMEI);
}

public void OnClickGetData(View view){
    final Handler hnd = new Handler();
    final Runnable r = new Runnable(){
        public void run(){

            SoapObject obj = new SoapObject(NAMESPACE,METHOD_NAME2);
            SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelop.setOutputSoapObject(obj);
            HttpTransportSE androidHTTP = new HttpTransportSE(URL,7000);
            try{
                androidHTTP.call(SOAP_ACTION2, envelop);
            }catch(IOException e){
                e.printStackTrace();
            }catch(XmlPullParserException e){
                e.printStackTrace();
            }

            try {                    
                @SuppressWarnings("unchecked")
                java.util.Vector<String> result11 = (java.util.Vector<String>)envelop.getResponse(); // to get List of Strings from the SoapObject.. then
                final ArrayList<String> prjList = new ArrayList<String>();
                for(String cs : result11)
                {
                prjList.add(cs);
                }
                hnd.post(new Runnable(){
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        for(int i = 0; i <prjList.size();i++){
                            //arrString[i] = reques.toString();
                            String Nu = prjList(i).toString();
                            Toast.makeText(InsertData.this,Nu , Toast.LENGTH_LONG).show();
                            try {
                                Thread.sleep(time);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                    }

                });
            } catch (SoapFault e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    };
}

public void OnClickSetData(final View view)
{
    final Handler hnd = new Handler();
    final Runnable ru = new Runnable(){
        //@SuppressWarnings("deprecation")
        @Override
        public void run() {
            // TODO Auto-generated method stub
            SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
            PropertyInfo pi = new PropertyInfo();
            PropertyInfo pi2 = new PropertyInfo();
            pi.namespace = NAMESPACE;
            pi.setName("Date");
            pi2.setName("IMEI_NO");
            pi.setValue(DateTime);
            pi2.setValue(IMEI);
            pi.setType(String.class);
            pi2.setType(String.class);
            request.addProperty(pi);
            request.addProperty(pi2);
            SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelop.dotNet = true;
            envelop.setOutputSoapObject(request);
            HttpTransportSE androidHttp = new HttpTransportSE(URL,7000);

            try
            {
                androidHttp.call(SOAP_ACTION, envelop);                 
            }
            catch(IOException e)
            {
                e.printStackTrace();
            } 
            catch (XmlPullParserException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    new Thread(ru).start();
}

}

这是我的Web服务,它是在.NET中创建的,当我在浏览器中检索数据时,它会检索数据,但不会显示在android应用程序中

 public string[] ThrowData()
    {
        string ConnectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();
        SqlCommand cmd = new SqlCommand();
        List<string> data = new List<string>();
        using (SqlConnection con =  new SqlConnection(ConnectionString))
        {
            con.Open();
            cmd.CommandText = "SELECT IMEI_NO FROM MAD";
            cmd.Connection = con;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                data.Add(dr["IMEI_NO"].ToString());
            }
        }

        return data.ToArray();
    }

有一个Log语句,看看它是否进入循环并接收到期望的值。如果它这样做但没有显示Toast,那是因为您没有在UI线程上显示Toast。如果您想在UI上显示任何更改,您需要在UI线程中进行更改。要在此处显示toast,可以使用runOnUiThread。例如:

YourActivityName.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(InsertData.this,Nu , Toast.LENGTH_LONG).show();
    }
});

编辑:要了解更多关于Log语句的信息,请参阅Android Log.v()、Log.d(?和http://developer.android.com/reference/android/util/Log.html

希望这能有所帮助。

最新更新