使用OkHttp库Android制作SOAP请求

时间:2018-01-21 13:13:54

标签: android soap retrofit okhttp3

我正在尝试通过OkHttp库进行SOAP调用,因此我可以升级到Retrofit Library使用。 我已经完成了帖子。

OkHttp。链接

Gradle中使用的库

compile 'com.squareup.okhttp3:okhttp:3.4.1'

我已成功拨打电话,但响应代码为415。 我已将内容类型更改为

已经过测试

.addHeader("Content-Type", "text/xml; charset=utf-8")

.addHeader("Content-Type", "application/json"; charset=utf-8")

.addHeader("Content-Type", "html/text"; charset=utf-8")

可能是什么问题

我的请求格式

final Request request = new Request.Builder()
                .url(URL)
                .addHeader("Content-Type", "text/xml; charset=utf-8")
                .addHeader("soapaction", "http://tempuri.org/Login")
                .post(body)
                .build();

其他代码:

final OkHttpClient client = new OkHttpClient();
        soapRequest = Login(userName.getText().toString(),password.getText().toString());
        RequestBody body = RequestBody.create(SOAP_MEDIA_TYPE, soapRequest);

        final Request request = new Request.Builder()
                .url(URL)
                .addHeader("Content-Type", "text/xml; charset=utf-8")
                .addHeader("soapaction", "http://tempuri.org/Login")
                .post(body)
                .build();


        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                String mMessage = e.getMessage().toString();
                Log.w("failure Response", mMessage);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String mMessage = response.body().string();

                Log.i("",mMessage);

             //   Toast.makeText(MainActivity.this,"Result :"+mMessage,Toast.LENGTH_LONG).show();

                //code = response.code();
              //  getResponse(mMessage, response);

            }
        });

功能登录

 public  String Login(String name,String password) {

        String body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "  <soap:Body>\n" +
                "    <Login xmlns=\"http://tempuri.org/\">\n" +
                "      <Name>"+userName+"</Name>\n" +
                "      <Password>"+password+"</Password>\n" +
                "    </Login>\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";

        return body;
    }

1 个答案:

答案 0 :(得分:2)

尝试以下代码

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("text/xml");

String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
            "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
            " <soap:Body>\n" + " <Login xmlns=\"http://tempuri.org/\">\n" +
            " <Name>"+userName+"</Name>\n" + " <Password>"+password+"</Password>\n" +
            " </Login>\n" + " </soap:Body>\n" + "</soap:Envelope>";

RequestBody body = RequestBody.create(mediaType, soap);
Request request = new Request.Builder()
            .url(YOUR_LINK)
            .post(body)
            .addHeader("content-type", "text/xml")
            .build();

Response response = client.newCall(request).execute();