简体   繁体   English

如何从Web服务检索字符串数组并在android中显示在Toast中

[英]How to retrieve String array from Webservice and show in toast in android

I want to retrieve array of string from my webservice and have to show it in my android project in Toast I coded it like this but its not showing anything can anyone please help me out :( 我想从我的检索字符串数组webservice ,并有显示它在我的Android项目Toast我编写它像这样,但它不是显示任何任何人都可以请帮我:(

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();

}

} }

and here is my webservice which is made in .NET its retriving data when im retrieving it in my browser but not showing in android application 这是我的网络服务,它是在.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();
    }

Have a Log statement to see if its entering the loop and is receiving the expected value. 有一个Log语句以查看其是否进入循环并正在接收期望值。 If it does that but does not show the Toast , its because you are not showing the Toast on the UI thread. 如果这样做但不显示Toast ,则是因为您没有在UI线程上显示Toast If you want to show any change on the UI, you need to do them in UI thread. 如果要在UI上显示任何更改,则需要在UI线程中进行更改。 To show toast here, you may use runOnUiThread . 为了在这里展示敬酒,您可以使用runOnUiThread Eg: 例如:

YourActivityName.runOnUiThread(new Runnable() {

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

Edit: For knowing more about Log statements see Android Log.v(), Log.d(), Log.i(), Log.w(), Log.e() - When to use each one? 编辑:要了解有关Log语句的更多信息,请参阅Android Log.v(),Log.d(),Log.i(),Log.w(),Log.e()-何时使用每一个? and http://developer.android.com/reference/android/util/Log.html http://developer.android.com/reference/android/util/Log.html

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM