【问题标题】:Android use WCF(webservice)Android 使用 WCF(webservice)
【发布时间】:2016-04-16 22:39:47
【问题描述】:

我是安卓开发的大一新生。今天我使用KSOAP2来使用我在服务器上完成的WCF。首先,我尝试在 Windows 窗体中使用 WCF。它运行正常,数据已上传。然后我将 WCF 与 KSOAP2 一起使用。字符串不能很好地发送,错误是: 方法抛出“org.ksoap2.SoapFault”异常。 错误的详细信息是: a:内部服务故障 值不能为空。 参数名称:s

我在服务器程序和安卓程序中没有名为“s”的参数。 .NET 的愿景是框架 4.0。 如果我使用 .NET 框架 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的问题。

【问题讨论】:

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


    【解决方案1】:

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

    这就是我正在做的事情。

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

    在你的异步任务中调用 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 2015-02-14
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多