【发布时间】:2018-06-30 05:35:14
【问题描述】:
我正在尝试通过 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;
}
【问题讨论】:
-
我认为有问题 soapRequest = Login(userName.getText().toString(),password.getText().toString());请尝试修复它stackoverflow.com/a/34902666/2298357
-
请查看我的肥皂请求
-
你试过“Content-Type: application/soap+xml; charset=utf-8”吗?
-
仍然报同样的错误:415 Unsupported Media Type
-
如果我用 HTTP 请求尝试它,它工作正常......但不是 OkHttp 库
标签: android soap retrofit okhttp3