无法使用Android连接到Web服务

时间:2012-12-14 22:17:45

标签: android .net android-ndk

无法通过Android连接到Web服务 它没有访问网络服务代码 我试图制作线程,然后在那里运行我的代码 我从我的手机测试webservice链接,可以访问

以下是.net和android的代码: -

Webservice Code .net:强文

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

<WebService(Namespace:="http://tempuri.org/")> _
Public Class Service
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As Integer
        Return 0
    End Function

End Class

这是守则

 public class MainActivity extends Activity implements OnClickListener  {

    ImageButton btn1;
    ImageButton Save;
    private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
    private static final String MethodName = "HelloWorld";
    private static final String NameSpace = "http://tempuri.org/"; 
    private static final String URL = "http://172.20.10.2/WebSite9/Service.asmx";  
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         final Thread webser    = new Thread() {

            public void run() {

                try {
                  //  String project = titles.get(position - 1);

               //     CallWebService();
                 SoapObject request = new SoapObject(NameSpace, MethodName);         
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                   envelope.dotNet=true; 

                   // request.addProperty("TextToDisplay", "This is coming from android");
                 envelope.setOutputSoapObject(request); 

                  androidHttpTransport.call(SOAP_ACTION, envelope); 

                    SoapObject result = (SoapObject)envelope.getResponse(); 
                 //   Toast.makeText(MainActivity.this,result.toString(), Toast.LENGTH_LONG).show();

                } catch (Exception e) {

                  Log.e("tag", e.getMessage());

                }


            }

        };
        webser.start(); 

请帮忙!

2 个答案:

答案 0 :(得分:1)

您的代码似乎没问题

请确保: -

1-使用kso​​ap2-android-2.5.2.jar
2-转到buildpath&gt;添加外部jar并选择它 3-复制ksoap2-android-2.5.2.jar并将其粘贴到libs文件

重新启动应用程序并尝试?

答案 1 :(得分:0)

把这段代码:

SoapObject result = (SoapObject)envelope.bodyIn;

而不是

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

它将按预期与您合作。