0

我的目标是从 Android 设备调用 .Net 网络服务(.asmx)。

.Net Webservice 使用 .net 测试客户端和 SOAPUI 应用程序进行测试。

我正在使用 Eclipse 构建 android 应用程序 - 包括用于 Web 服务调用的 KSOAP2 项目。应用程序有android.permission.INTERNET标签包含在用户权限部分。

我有两个问题
[1] 如果我从(下面的代码)调用 webservice 方法,应用程序会抛出“android.os.NetworkOnMainThreadException”错误onCreate()(我在互联网上看到的许多示例都是相同的方法,但我没有知道为什么它在我的项目中失败)

[2] 为了解决上述问题,我在“MainActivity”中创建了一个类,但现在我在线收到“找不到源”消息client.execute("some text as input") (检查了构建路径,有对 KSOAP2 jar 文件的引用并且没有编译错误)

请问正确的方法是什么以及如何解决这个问题?谢谢你的帮助

[问题 1] 的代码

    //This code for calling .net webservice from Android devide.. and throws "android.os.NetworkOnMainThreadException" whilst calling.

    package com.example.wsclient;

    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.view.Menu;
    import android.widget.TextView;

    public class MainActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            String SOAP_ACTION = "http://mycomp.org/GetParticipantVote";
            String METHOD_NAME = "GetParticipantVote";
            String NAMESPACE = "http://mycomp.org/";
            String URL = "http://10.0.2.2:8080/WSRating.asmx";

            try {
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                request.addProperty("sID", "My_name");
                request.addProperty("sRate", "9");

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

                envelope.setOutputSoapObject(request);
                HttpTransportSE  androidHttpTransport = new HttpTransportSE (URL);
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapObject result=(SoapObject)envelope.getResponse();

                //To get the data.
                String resultData=result.getProperty(0).toString();

                AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                alertDialog.setTitle("Webservice Client");
                alertDialog.setMessage("Webservice Resp : " +  resultData);
                alertDialog.show();

            }
            catch (Exception eE) {

                AlertDialog alertDialog1 = new AlertDialog.Builder(this).create();
                alertDialog1.setTitle("Webservice Client");
                alertDialog1.setMessage("Error : " +  eE.getMessage());
                alertDialog1.show();
           }
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    }

[问题 2] 的代码——

            package com.example.wsclient;

            import org.ksoap2.SoapEnvelope;
            import org.ksoap2.serialization.SoapObject;
            import org.ksoap2.serialization.SoapSerializationEnvelope;
            import org.ksoap2.transport.HttpTransportSE;

            import android.os.Bundle;
            import android.app.Activity;
            import android.app.AlertDialog;
            import android.view.Menu;
            import android.widget.TextView;

            public class MainActivity extends Activity {
                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);

                    CallWS client = new CallWS();
                    client.execute("some text as input");

                }
                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                    getMenuInflater().inflate(R.menu.activity_main, menu);
                    return true;
                }

                    /* AsyncTask class to remove android.os.NetworkOnMainThreadException error */
                    private class CallWS extends AsyncTask<String, Integer, String> {

                         protected String doInBackground(String... sValues) {

                                String resultData;

                                String SOAP_ACTION = "http://mycomp.org/GetParticipantVote";
                                String METHOD_NAME = "GetParticipantVote";
                                String NAMESPACE = "http://mycomp.org/";
                                String URL = "http://10.0.2.2:8080/WSRating.asmx";

                                try {
                                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                                    request.addProperty("sID", "My_name");
                                    request.addProperty("sRate", "9");

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

                                    envelope.setOutputSoapObject(request);
                                    HttpTransportSE  androidHttpTransport = new HttpTransportSE (URL);
                                    androidHttpTransport.call(SOAP_ACTION, envelope);
                                    SoapObject result=(SoapObject)envelope.getResponse();

                                    //To get the data.
                                    resultData =result.getProperty(0).toString();
                                }
                                catch (Exception eE) {
                                    resultData = "Error : " +  eE.getMessage();
                               }

                               return resultData
                         }

                         protected void onProgressUpdate(Integer... values) {

                         }

                         protected void onPostExecute(String result) {
                             // do something here to display the result...
                         }
                     }

                     }
4

3 回答 3

0

您不能在主线程上执行网络操作。结帐
http://developer.android.com/reference/android/os/AsyncTask.html

于 2012-12-19T05:08:31.037 回答
0

我最好的猜测是您正在 ICS 上测试此解决方案。

您可以使用 Strick 模式进行快速测试:http: //developer.android.com/reference/android/os/StrictMode.html

在 ICS 中,禁止 UI 上的所有网络操作,这就是为什么您需要重建它以使用 AsyncTasks,http://developer.android.com/reference/android/os/AsyncTask.html

于 2012-10-03T12:25:56.400 回答
0

只需使用以下代码覆盖...

StrictMode.ThreadPolicy 策略 = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);

并且还使用...

于 2014-08-03T09:27:23.633 回答