首页 \ 问答 \ 在模拟器中使用kSOAP2使用localhost WCF服务(Consuming a localhost WCF service using kSOAP2 in emulator)

在模拟器中使用kSOAP2使用localhost WCF服务(Consuming a localhost WCF service using kSOAP2 in emulator)

我已经把头撞到了这堵墙上了几天,所以任何帮助都非常感激。

首先是一些背景知识。 我是一位经验丰富的WindowsMo​​bile开发人员,因此我是Java,Android甚至WCF的老手。 我已经做了很多关于如何使用kSOAP2在Android中使用WCF应用程序的研究,但无论我做得最好,我能想到的是由于超时而导致Android中的套接字错误。

首先,我在Visual Studio 2008中创建了一个不需要参数的Web服务应用程序,只需使用字符串值进行响应。 Web服务代码如下:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

namespace Sample
{
    [WebService(Namespace = "http://sample.com/")]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string SayHello()
        {
            return "Hello, Android. From your friend, WCF";
        }
    }
}

对于此Web服务,我没有调整任何其他设置或对Web.config文件进行任何修改。

当我运行该服务时,它会打开我的浏览器并指向以下网址:

http://localhost:61554/Service1.asmx

接下来,我跳入Eclipse并创建了一个简单的Android项目来使用WCF服务。 对于初学者,我更改了AndroidManifest.xml文件并将此语句添加到其中:

<uses-permission android:name="android.permission.INTERNET" />

现在我的Android类的代码应该是繁重的工作:

package com.sample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.ksoap2.*;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.*;

public class Sample extends Activity {
    TextView result;

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

        result = (TextView)findViewById(R.id.textViewResult);

        try {
            SoapObject Request = new SoapObject("http://sample.com/", "SayHello");

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

            HttpTransportSE androidHttpTransport = new HttpTransportSE("http://192.168.1.72:61554/Service1.asmx");

            androidHttpTransport.call("http://192.168.1.72:61554/SayHello", envelope);
            SoapObject response = (SoapObject)envelope.getResponse();
            String resultValue =  response.getProperty(0).toString();

            result.setText(resultValue);            
        } 
        catch (Exception e) {
            result.setText(e.getMessage());
        }
    }
}

给我错误的代码行是:

androidHttpTransport.call("http://192.168.1.72:61554/SayHello", envelope);

当在模拟器中运行时,代码到达该行,暂停一两分钟,然后进入具有超时异常的catch块。

关于我可能在这里缺少的任何想法? 感谢您的帮助。


I have rammed my head against this wall for a couple of days now so any help is very appreciated.

A bit of background, first. I am a seasoned WindowsMobile developer moving into Android so I am therefore a noob in Java, Android, and even WCF. I have done a lot of research on how to consume WCF apps in Android using kSOAP2 but no matter what I do the best I can come up with is a socket error in Android because of a time out.

First, I created a web service application in Visual Studio 2008 that requires no parameters and simply responds with a string value. The web service code is as follows:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

namespace Sample
{
    [WebService(Namespace = "http://sample.com/")]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string SayHello()
        {
            return "Hello, Android. From your friend, WCF";
        }
    }
}

For this web service I did not adjust any other settings or make any modifications to the Web.config file.

When I run the service it opens up my browser and points to the following url:

http://localhost:61554/Service1.asmx

Next, I jumped into Eclipse and created a simple Android project to consume the WCF service. For starters I changed the AndroidManifest.xml file and added this statement to it:

<uses-permission android:name="android.permission.INTERNET" />

Now for the code from my Android class that is supposed to do the heavy lifting:

package com.sample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.ksoap2.*;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.*;

public class Sample extends Activity {
    TextView result;

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

        result = (TextView)findViewById(R.id.textViewResult);

        try {
            SoapObject Request = new SoapObject("http://sample.com/", "SayHello");

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

            HttpTransportSE androidHttpTransport = new HttpTransportSE("http://192.168.1.72:61554/Service1.asmx");

            androidHttpTransport.call("http://192.168.1.72:61554/SayHello", envelope);
            SoapObject response = (SoapObject)envelope.getResponse();
            String resultValue =  response.getProperty(0).toString();

            result.setText(resultValue);            
        } 
        catch (Exception e) {
            result.setText(e.getMessage());
        }
    }
}

The line of code that is giving me the error is:

androidHttpTransport.call("http://192.168.1.72:61554/SayHello", envelope);

When run in the emulator the code gets to that line, pauses for a minute or two, and then falls into the catch block with a time out exception.

Any ideas on what I might be missing here? The help is appreciated.


原文:https://stackoverflow.com/questions/6538055
更新时间:2023-10-14 18:10

最满意答案

您可以使用Andlytics执行此操作 ,但免费版本不允许通知(需要支付2.99美元)。 虽然免费的方式确实可以通过一键式方式查看您的统计数据和评级,而无需调用我认为非常有用的Web浏览器/日志记录。


You can do this with Andlytics, but the free version doesn't allow notifications (need to pay $2.99 for that). Though the free one does have a one-touch way to view your stats and ratings without invoking a web browser/logging in which I find pretty useful.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。