English 中文(简体)
消费的周转基金在机器人和机器人中的使用服务
原标题:Consuming WCF service in android
  • 时间:2012-05-25 04:57:56
  •  标签:
  • android
  • wcf

我创建了这项服务(使用WCF、Azure):

[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class Service1 : IPMPService
{
    public int Dummy()
    {
        return 0;
    }
}

IPMP服务是:

[ServiceContract]
public interface IPMPService
{
    [WebGet()]
    [OperationContract]
    int Dummy();
}

And attempted to consume it in my android app:
1st attempt:

String METHOD_NAME = "DummyRequest";
String NAMESPACE = "http://tempuri.org/";
String URL = "http://tyty.cloudapp.net/Service1.svc";
String SOAP_ACTION = "http://tyty.cloudapp.net/Service1.svc/Dummy";

String res = "";
try {
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.call(SOAP_ACTION, envelope);
    SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
    // to get the data
    String resultData = result.toString();
    res = resultData;
    // 0 is the first object of data
    } catch (Exception e) {
        res = e.getMessage();
    }   

结果: SoapFault - 错误代码: a: ActionNotsupported faultstries: a: ActionNotsupported: the message with Action http://tyty.cloudappp.net/Servicice1.svc/Dummy 无法在接收器上处理电文,因为终端Dispatcher的合同Filter不匹配。这可能是因为合同不匹配(发件人和接收人之间有片断动作),也可能是因为发件人和接收人之间有约束性/安全不匹配。检查发件人和接收人有相同的合同和相同的约束性(包括安全要求,如电文、运输、无)。

第二次尝试(取自"http://romenlaw.blogspot.com/2008/08/pot-web-services- from-android.html" rel=“不跟随 noreferrer">这里):

String SERVER_HOST = "http://tyty.cloudapp.net";
int SERVER_PORT = 8080;
String URL1 = "/Service1.svc/Dummy";
String keywords = null;
HttpEntity entity = null;
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(URL1);
try {
HttpResponse response = client.execute(target, get);
entity = response.getEntity();
keywords = EntityUtils.toString(entity);
} catch (Exception e) {
e.printStackTrace();
   } finally {
if (entity != null)
        try {
        entity.consumeContent();
    } catch (IOException e) {
  }
}  

结果: java. net. unnownHostExpusion: 无法解析主机“ http://tyty. cloudapp. net ” : 没有与主机名相关的地址

我还在 c# 中创建了一个( 工作) 客户端, 以下是它的 App. config :

<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IPMPService" closeTimeout="00:01:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
            allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
            useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://tyty.cloudapp.net/Service1.svc"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPMPService"
          contract="IPMPService" name="BasicHttpBinding_IPMPService" />
    </client>
  </system.serviceModel>
</configuration>

As mentioned, both attempts in Android failed. Can anyone please tell me what I m doing wrong, and how to do it right?
Thanks.

最佳回答

你第一次尝试就差一点了,只需要两个办法:

String METHOD_NAME = "Dummy";
String NAMESPACE = "http://tempuri.org/";
String URL = "http://tyty.cloudapp.net/Service1.svc";
String SOAP_ACTION = "http://tempuri.org/IPMPService/Dummy";

String res = "";
try {
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.call(SOAP_ACTION, envelope);
    SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
    // to get the data
    String resultData = result.toString();
    res = resultData;
    // 0 is the first object of data
} catch (Exception e) {
    res = e.getMessage();
}   

第一个是方法名称:它只是 Dummy (而不是 Dummy请求 ),第二个是SOAP行动:它有 http://tempuri.org/IPMPService/Dummy

两者都可以来自 WSDL。 您的操作定义为:

<wsdl:operation name="Dummy">
    <wsdl:input wsaw:Action="http://tempuri.org/IPMPService/Dummy" message="tns:IPMPService_Dummy_InputMessage"/>
    <wsdl:output wsaw:Action="http://tempuri.org/IPMPService/DummyResponse" message="tns:IPMPService_Dummy_OutputMessage"/>
</wsdl:operation>

如果您再查看 IPMPService_Dummy_InputMessage 信件类型,该元素被称为 Dummy :

<wsdl:message name="IPMPService_Dummy_InputMessage">
    <wsdl:part name="parameters" element="tns:Dummy"/>
</wsdl:message>

SOAP行动在这里发现:

<wsdl:operation name="Dummy">
    <soap:operation soapAction="http://tempuri.org/IPMPService/Dummy" style="document"/>
    ...
问题回答
public class RESTConnection extends Global 
{


    Global objglobal=Global.getInstance();

    private static String SERVICE_URI = "your service path";

    public JSONObject GetCashierLogin(String UserName, String Password)
    {
        new ArrayList<String>();  

        JSONObject jObj = null;  
        try 
        {

            SERVICE_URI=objglobal.getConnectionString();
            DefaultHttpClient httpClient = new DefaultHttpClient(); 
            HttpGet request = new HttpGet(SERVICE_URI+ "/LoginByUserIdAndPassword?Username=" + UserName + "&password=" + Password + "");
            request.setHeader("Accept", "application/json");
            request.setHeader("Content-type", "application/json");
            HttpResponse response = httpClient.execute(request);
            HttpEntity responseEntity = response.getEntity();
            String bufferLogin = EntityUtils.toString(responseEntity, HTTP.UTF_8);
            JSONArray jsonArray = new JSONArray(bufferLogin);

            if (jsonArray != null)
            {
                for (int i = 0; i < jsonArray.length(); i++)
                {
                    jObj=(JSONObject) jsonArray.get(i);

                }
            }

        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return jObj;
    }




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签