【发布时间】:2020-10-26 13:56:07
【问题描述】:
我必须为一个小项目使用 SOAP api。我现在可以检索数据,但是当我尝试插入数据时,它似乎不起作用并给我这个错误:服务器无法处理请求。 ---> 对象引用未设置为对象的实例。
我正在为 post-request 使用以下 soap 资源:http://webapp.tcscourier.com/codapi/Service1.asmx?op=InsertData
我有以下代码:
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("text/xml");
RequestBody body = RequestBody.create(mediaType, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\r\n <soap12:Body>\r\n <InsertData xmlns=\"http://202.61.51.93:6265/\">\r\n <userName>string</userName>\r\n <password>string</password>\r\n <costCenterCode>string</costCenterCode>\r\n <consigneeName>string</consigneeName>\r\n <consigneeAddress>string</consigneeAddress>\r\n <consigneeMobNo>string</consigneeMobNo>\r\n <consigneeEmail>string</consigneeEmail>\r\n <originCityName>string</originCityName>\r\n <destinationCityName>string</destinationCityName>\r\n <pieces>string</pieces>\r\n <weight>string</weight>\r\n <codAmount>0.2</codAmount>\r\n <custRefNo>string</custRefNo>\r\n <productDetails>string</productDetails>\r\n <fragile>string</fragile>\r\n <services>string</services>\r\n <remarks>string</remarks>\r\n <insuranceValue>string</insuranceValue>\r\n </InsertData>\r\n </soap12:Body>\r\n</soap12:Envelope>");
Request request = new Request.Builder()
.url("http://webapp.tcscourier.com/codapi/Service1.asmx?op=InsertData")
.method("POST", body)
.addHeader("Content-Type", "text/xml")
.build();
try {
Response response = client.newCall(request).execute();
JSONObject xmlJSONObj = XML.toJSONObject(response.body().string());
System.out.println(xmlJSONObj.toString());
} catch (IOException e) {
e.printStackTrace();
}
我错过了什么?
【问题讨论】: