3 Temmuz 2013 Çarşamba

how can i connect to webservice from android

hi.

    private static String SOAP_ACTION = "http://tempuri.org/HelloWorld";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String METHOD_NAME = "HelloWorld";
    private static final String URL = "http://192.168.1.3:80/tryAndoridWebService/serviceTest.asmx?WSDL";

    Button btnConnectWebServis;
    EditText txtGetData;

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        btnConnectWebServis= (Button)findViewById(R.id.btnConnectWebServis);
        txtGetData= (EditText)findViewById(R.id.txtGetData);


        btnConnectWebServis.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //Initialize soap request + add parameters
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                //Use this to add parameters
                request.addProperty("param1","my parameter");// send parameter to webservice

                //Declare the version of the SOAP request
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

                envelope.setOutputSoapObject(request);
                envelope.dotNet = true;

                try {
                    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                    StrictMode.setThreadPolicy(policy);
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                    androidHttpTransport.call(SOAP_ACTION, envelope);

                    if (envelope.bodyIn instanceof SoapFault) {
                        String str= ((SoapFault) envelope.bodyIn).faultstring;
                        txtGetData.setText(str);
                        Log.i("", str);
                    } else {
                        SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
                        Log.d("WS", String.valueOf(resultsRequestSOAP));
                        txtGetData.setText(String.valueOf(resultsRequestSOAP.getProperty(0).toString()));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "hata : "+e.getMessage(),Toast.LENGTH_LONG).show();
                }
            }
        });
}

one more thing
add your manifest.xml file
<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET" />

AND 
run this code on your adb console
 adb forward tcp:8080 tcp:8080

Hiç yorum yok:

Yorum Gönder