Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making SOAP Request using OkHttp library Android

I am trying to make SOAP Call via OkHttp library so I can upgrade to Retrofit Library use . I have gone through said post .

OkHttp . Link

Library Used in Gradle

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

I have successfully made the call , but the Response Code comes as 415 . I have changed the content type as

Have tested with

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

and

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

and

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

also

What could be the problem

My Request Format

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();

Other Code :

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);

            }
        });

Function Login

 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;
    }
like image 843
Yatin Avatar asked Jan 21 '18 13:01

Yatin


1 Answers

Try the below code

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();
like image 172
user3805333 Avatar answered Oct 23 '22 05:10

user3805333