我的代码如下
portpackage com.demo;
import android.util.Log;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
/**
* Created by shaileshp on 15-Mar-18.
*/
public class HttpUtils {
private static final MediaType SOAP_MEDIA_TYPE = MediaType.parse("text/xml");
private static final String soap_action = "urn:microsoft-dynamics-schemas/codeunit/HelloWorld:PrintHello";
private static final String namespace = "urn:microsoft-dynamics-schemas/codeunit/HelloWorld";
private static final String method_name = "PrintHello";
public static void doGetRequest() throws IOException {
SoapObject soapObject = new SoapObject(namespace, method_name);
soapObject.addProperty("Username", "ravia");
soapObject.addProperty("Password", "Ravi@123");
soapObject.addProperty("Domain", "techcronus");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
OkHttpClient okHttpClient = new OkHttpClient.Builder().authenticator(new NTLMAuthenticator("ravia", "Ravi@123", "techcronus", "")).build();
RequestBody body = RequestBody.create(SOAP_MEDIA_TYPE, String.valueOf(envelope.getResponse()));
Request request = new Request.Builder()
.url("http://192.168.1.199:1013/MISReports/WS/Test/Codeunit/HelloWorld")
.post(body)
.addHeader("Content-Type", "text/xml; charset=utf-8")
.addHeader("soapaction", soap_action)
.build();
// okhttp3.Response response = okHttpClient.newCall(request).execute();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(HttpUtils.class.getSimpleName(), "onFailure: " + e.getMessage());
}
@Override
public void onResponse(Call call, okhttp3.Response response) throws IOException {
Log.e(HttpUtils.class.getSimpleName(), "onResponse: " + response.body().string());
}
});
// return response.body().string();
}
}
当我在那时运行应用程序时,我收到了像
这样的回复<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="urn:microsoft-dynamics-schemas/error">a:Microsoft.Dynamics.Nav.Service.WebMetadata.ServiceBrokerException
</faultcode>
<faultstring xml:lang="en-US">Soap message is invalid!
</faultstring>
<detail>
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Soap message is invalid!
</string>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
我正试图从okhttp得到回应。 请帮助我找出实际问题的位置以及我必须进行更改以成功运行soap操作并从中获取响应。
请帮助我解决问题的任何人。