0

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 :(

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 i m retrieving it in my browser but not showing in android application

 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();
    }
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124

1 Answers1

0

Have a Log statement to see if its entering the loop and is receiving the expected value. If it does that but does not show the Toast, its because you are not showing the Toast on the UI thread. If you want to show any change on the UI, you need to do them in UI thread. To show toast here, you may use 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? and http://developer.android.com/reference/android/util/Log.html

Hope this helps.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124