如何从Webservice中检索String数组并在android中显示toast

时间:2013-11-24 06:42:37

标签: java android eclipse 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();

}

}

这是我的网络服务,它是在.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();
    }

1 个答案:

答案 0 :(得分:0)

有一个Log语句,以查看它是否进入循环并且正在接收预期值。如果它这样做但没有显示Toast,那是因为你没有在UI线程上显示Toast。如果要在UI上显示任何更改,则需要在UI线程中执行这些更改。要在此处显示吐司,您可以使用runOnUiThread。例如:

YourActivityName.runOnUiThread(new Runnable() {

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

修改 要了解有关Log语句的详情,请参阅Android Log.v(), Log.d(), Log.i(), Log.w(), Log.e() - When to use each one?http://developer.android.com/reference/android/util/Log.html

希望这有帮助。