Android使用WCF(webservice)

时间:2016-04-16 22:39:47

标签: c# android .net web-services wcf

我是android开发的新手。今天我使用KSOAP2来使用我在服务器上完成的WCF。首先,我尝试在Windows窗体中使用WCF。它运行正常,数据已上传。然后我使用WCF和KSOAP2。字符串不能很好地发送,错误是: 方法抛出'org.ksoap2.SoapFault'异常。 错误的缺点是: 答:InternalServiceFault 值不能为空。 参数名称:s

我在服务器程序和android程序中没有名为's'的参数。 .NET的愿景是框架4.0。 如果我使用.NET framework 4.5,那么android可以将它与KSOAP2一起使用。 但是,我必须使用4.0 我该如何解决这个问题? 欢呼声。

android中的代码如下所示:

 transferthread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true)
                {

                        SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
                        int a = 1;
                        request.addProperty("userid",a);
                        request.addProperty("healthData",info);
                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
                        envelope.dotNet = true;
                        envelope.bodyOut = request;
                        envelope.setOutputSoapObject(request);
                        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                        androidHttpTransport.debug = true;
                        try {
                            androidHttpTransport.call(SOAP_ACTION, envelope);
                           // final SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
                            envelope.getResponse();
                            Log.e("str",envelope.getResponse().toString());
                            a=1;
                            //Log.e("aaa",envelope.getResponse().toString());
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (XmlPullParserException e) {
                            e.printStackTrace();
                        }



                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }


                }

            }
        });
        transferthread.start();

我认为是ksoap2问题。

1 个答案:

答案 0 :(得分:0)

我的答案是关于如何实现Android使用WCF的问题的另一种替代解决方案。我曾经尝试过KSOAP2。出于某种原因(我忘了)我放弃了使用它。

我正在做同样的事情。

您可以安装Wizdler(Chrome扩展程序)来生成信封。并将粘贴复制到信封代码。

在你的asynctask中调用getYourData。

public ArrayList<YourData> getYourData(String username, String password) {
ArrayList<YourData> resultList;
String resultData;
try {
    //Put your envelope here.
    final String envelope = "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
            "    <Body>\n" +
            "        <Something xmlns=\"http://www.example.com/RemoteServices/url/\">\n" +
            "            <!-- Optional -->\n" +
            "            <request>\n" +
            "                <Authentication xmlns=\"http://schemas.datacontract.org/RemoteServicesv2.Core\">\n" +
            "                    <Password>" +
            password +
            "</Password>\n" +
            "                    <Username>" +
            username +
            "</Username>\n"
            "        </Something>\n" +
            "    </Body>\n" +
            "</Envelope>";

    resultData = CallWebService(URL, "http://www.example.com/webserviceURL", envelope);
    Log.e("resultData for Something", ""+resultData);
    resultList = parseFunction(resultData);
} catch (Exception e) {
    resultList = null;
}
return resultList;

}

// How to send SOAP envelope to web service
private String CallWebService(String url,
                              String soapAction,
                              String envelope) {
    final DefaultHttpClient httpClient = new DefaultHttpClient();
    // request parameters

    HttpParams params = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 20000);
    HttpConnectionParams.setSoTimeout(params, 25000);
    // set parameter
    HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);

    // POST the envelope
    HttpPost httppost = new HttpPost(url);
    // add headers
    httppost.setHeader("soapaction", soapAction);
    httppost.setHeader("Content-Type", "text/xml; charset=utf-8");

    String responseString = "";
    try {

        // the entity holds the request
        HttpEntity entity = new StringEntity(envelope);
        httppost.setEntity(entity);

        // Response handler

        ResponseHandler<String> rh = new ResponseHandler<String>() {
            // invoked when client receives response

            public String handleResponse(HttpResponse response)
                    throws ClientProtocolException, IOException {

                // get response entity
                HttpEntity entity = response.getEntity();


                // read the response as byte array
                StringBuffer out = new StringBuffer();
                byte[] b = EntityUtils.toByteArray(entity);

                // write the response byte array to a string buffer
                out.append(new String(b, 0, b.length));

                return out.toString();
            }
        };

        responseString = httpClient.execute(httppost, rh);


    } catch (Exception e) {
        Log.v("exception", e.toString());
    }

    String xml = responseString.toString();
    // close the connection
    System.out.println("xml file ------" + xml);
    httpClient.getConnectionManager().shutdown();
    return responseString;
}