如何在android中的svc(soap)服务中发送请求发布方法?

时间:2015-02-19 03:25:02

标签: android soap

我不知道如何在肥皂服务中以post方式发送请求。请有人帮我解决此问题。

到目前为止,这是我的代码。

//Code
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "Login";
String SOAP_ACTION = "http://tempuri.org/Login";
String URL = "http://hostname/AllServices.svc/Login";

protected void soap() {
    // TODO Auto-generated method stub

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    // Set all input params
    request.addProperty("sapcode", "3232323");
    request.addProperty("password", "3232323");
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER12);
    // Enable the below property if consuming .Net service
    envelope.dotNet = true;
    envelope.encodingStyle = SoapEnvelope.ENC;
    envelope.setAddAdornments(false);
    envelope.implicitTypes = false;
    envelope.setOutputSoapObject(request);

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try {
        envelope.headerOut = new Element[1];
        envelope.headerOut[0] = buildAuthHeader();

        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject response = (SoapObject) envelope.getResponse();
        Log.i("response", response.getProperty(0).toString());
        // response.getProperty(0).toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

private Element buildAuthHeader() {
    Element h = new Element().createElement(NAMESPACE, "AuthHeader");
    Element username = new Element().createElement(NAMESPACE, "User Name");
    username.addChild(Node.TEXT, "tech");
    h.addChild(Node.ELEMENT, username);
    Element pass = new Element().createElement(NAMESPACE, "Password");
    pass.addChild(Node.TEXT, "tech@001");
    h.addChild(Node.ELEMENT, pass);

    return h;
}

基本标头身份验证: 来自API的每个方法调用都需要基本的头认证

用户名:

密码:

方法类型:POST

返回类型为json格式。

1 个答案:

答案 0 :(得分:0)

try {

            // Build JSON string
            JSONStringer loginCredentials = new JSONStringer().object()
                    .key("sapcode").value(userNameSend).key("password")
                    .value(passwordSend).endObject();

            JSONObject jsonObj = new ServerResponse(
                    Urlgenerator.loginCheck()).getJSONObjectfromURL(
                    RequestType.POST, loginCredentials);

            if (jsonObj != null) {
                JSONObject loginObj = jsonObj.getJSONObject("Login");
                if (loginObj != null) {
                    String errorcode = loginObj.getString("errorcode");
                    final String result = loginObj.getString("result");
                    if (errorcode.equalsIgnoreCase("0")) {
                        Utils.updateSharedPreference("remember", "true");
                        Utils.updateSharedPreference("sapcode",
                                userNameSend);
                        startActivity(new Intent(LoginScreen.this,
                                HomeScreen.class));
                        finish();
                    } else {

                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                ToastUtils.displayToast(
                                        ToastTypeEnum.ALERT_TOAST, result,
                                        Toast.LENGTH_SHORT);
                            }
                        });

                    }
                }
            }

            pDialog.dismiss();

        } catch (Exception e) {

            pDialog.dismiss();

        }