调用从Android应用程序的Web服务 [英] calling a web service from an android application

查看:113
本文介绍了调用从Android应用程序的Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code,我互联网上找到调用从我的Andr​​oid应用程序的Web服务:

I am using the following code which i found on internet to call a web service from my android app:

public class WebServiceActivity extends Activity {
    private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";

    private static final String METHOD_NAME = "HelloWorld";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://192.168.1.19/TestWeb/WebService.asmx";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



        Button getquote = (Button) findViewById(R.id.getquote);  
        getquote.setOnClickListener(new OnClickListener() {  

        public void onClick(View v) { 
            TextView result1;
            result1=(TextView)findViewById(R.id.result1);
        try {


            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            EditText CompanyName = (EditText) findViewById(R.id.CompanyName); 
            String val1 = (CompanyName.getText().toString());
            request.addProperty("passonString", val1);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet=true;
            envelope.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);

            Object result = (Object)envelope.getResponse();

            result1.setText(result.toString());
        } catch (Exception e) {

            result1.setText(e.getMessage());
            }
    }
});

}

}

我收到错误的应用不是responding.I是外地的新的,请大家帮忙。
在此先感谢

I am getting error that application is not responding.I am new in the field , please help. Thanks in advance

推荐答案

把你的Web服务请求作为后台任务。通常是由网络活动所用的时间是联合国predictable。如果你使用上面,UI线程将阻塞。这就是为什么它说:应用程序没有响应。

Put your web service request as a background task. Usually time taken by a network activity is unpredictable. If you use above, the UI Thread will block. That is why it says "Application not responding".

我发现这篇大文章。
http://www.vogella.de/articles/AndroidPerformance/article.html#concurrency
看看2.后台处理

I found this great article. http://www.vogella.de/articles/AndroidPerformance/article.html#concurrency Have a look at 2. Background Processing

您的例子可以修改如下。

Your example can be modified as following.

public class WebServiceActivity extends Activity {
    private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";

    private static final String METHOD_NAME = "HelloWorld";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://192.168.1.19/TestWeb/WebService.asmx";

    TextView result1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        result1 = (TextView) findViewById(R.id.result1);

        Button getquote = (Button) findViewById(R.id.getquote);
        getquote.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                // push soap request into background.
                new Thread(new Runnable(){

                    @Override
                    public void run() {
                        doSoapRequest();
                    }

                },"DOINBACKGROUND");
            }
        });

    }

    private void doSoapRequest(){
        try {

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            EditText CompanyName = (EditText) findViewById(R.id.CompanyName);
            String val1 = (CompanyName.getText().toString());
            request.addProperty("passonString", val1);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(
                    URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);

            Object result = (Object) envelope.getResponse();

//          result1.setText(result.toString());
//           update UI data in a Handler
            Message msg = new Message();
            msg.obj = result.toString();
            result1Handler.sendMessage(msg);
        } catch (Exception e) {

//          result1.setText(e.getMessage());
//           update UI data in a Handler
            Message msg = new Message();
            msg.obj = e.getMessage();
            result1Handler.sendMessage(msg);
        }
    }

    private Handler result1Handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            result1.setText(msg.obj.toString());
        }

    };

}

这篇关于调用从Android应用程序的Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆