Yat*_*tin 5 android soap retrofit okhttp3
我试图通过OkHttp库进行SOAP调用,以便可以升级到Retrofit库使用。我已经看过上述帖子。
OkHttp。链接
Gradle中使用的库
compile 'com.squareup.okhttp3:okhttp:3.4.1'
Run Code Online (Sandbox Code Playgroud)
我已经成功拨打了电话,但是响应代码为415。我将内容类型更改为
经过测试
.addHeader("Content-Type", "text/xml; charset=utf-8")
Run Code Online (Sandbox Code Playgroud)
和
.addHeader("Content-Type", "application/json"; charset=utf-8")
Run Code Online (Sandbox Code Playgroud)
和
.addHeader("Content-Type", "html/text"; charset=utf-8")
Run Code Online (Sandbox Code Playgroud)
也
可能是什么问题呢
我的要求格式
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();
Run Code Online (Sandbox Code Playgroud)
其他代码:
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);
}
});
Run Code Online (Sandbox Code Playgroud)
功能登录
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;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
试试下面的代码
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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3218 次 |
| 最近记录: |