我想调用一个使用XML数据的API(@POST)。而我似乎不知道如何发送' - '。我学会了如何使用Json,但与XML不同
我搜索了3/4天,我找不到适合我的解决方案:/
以下是我需要发送的请求:
<qosFeatureData xmlns:qos="urn:oma:xml:rest:netapi:qos:1">
<clientCorrelator>v1234</clientCorrelator>
<predefinedQosFeatureId>Pred_Coca</predefinedQosFeatureId>
<media>
<mediaNumber>1</mediaNumber>
<mediaType>Other</mediaType>
<ipFlow>
<flowNumber>1</flowNumber>
<flowDescription>
<direction>Downlink</direction>
<protocol>6</protocol>
<otherPartyIpAddress>
<ipV4Address>X.X.X.X</ipV4Address>
</otherPartyIpAddress>
<otherPartyPortNumber>
<port>XX</port>
</otherPartyPortNumber>
<userIpAddress>
<ipV4Address>Y.Y.Y.Y</ipV4Address> ?????
</userIpAddress>
</flowDescription>
<flowDescription>
<direction>Uplink</direction>
<protocol>6</protocol>
<otherPartyIpAddress>
<ipV4Address>X.X.X.X</ipV4Address>
</otherPartyIpAddress>
<otherPartyPortNumber>
<port>XX</port>
</otherPartyPortNumber>
<userIpAddress>
<ipV4Address>Y.Y.Y.Y</ipV4Address> ?????
</userIpAddress>
</flowDescription>
<flowStatus>Enabled</flowStatus>
</ipFlow>
<flowStatus>Enabled</flowStatus>
</media>
我已经设置了“Post”调用的代码(其中没有任何内容),我得到了一个回复(不是很好的回复:500,但我认为这是因为我的数据没有被发送)
这是我的界面代码
public interface ApiHttpPost {
@Headers({"Content-Type: application/xml; charset=utf-8"})
@POST("confidential")
Call<qosFeature> getqosFeature (@Header("Authorization") String authHeader);
}
这是我的主要活动
private void AppelHttpPost() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("CONFIDENTIAL")
.client(getUnsafeOkHttpClient())
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
ApiHttpPost service = retrofit.create(ApiHttpPost.class);
String userName = "CONFIDENTIAL";
String passWord = "CONFIDENTIAL";
String base = userName + ":" + passWord;
String authHeader = "Basic " + Base64.encodeToString(base.getBytes(), Base64.NO_WRAP);
Call<qosFeature> qosFeatureCall = service.getqosFeature(authHeader);
qosFeatureCall.enqueue(new Callback<qosFeature>() {
@Override
public void onResponse(Call<qosFeature> call, Response<qosFeature> response) {
int statusCode = response.code();
qosFeature qosFeature = response.body();
Log.d(TAG, "onResponse: " + statusCode);
}
@Override
public void onFailure(Call<qosFeature> call, Throwable t) {
Log.d(TAG, "onFailure: " + t.getMessage());
}
});
}
感谢您的帮助:s
编辑:我回来再次请求你的帮助,Potapov Anton提出的解决方案在我的案例中并没有真正起作用。在Android studio上,在调试模式下我看到了一些好东西! (Screeshot)但是当我在Wireshark上看它并不好时,我不想要的字符串/字符串+ xml看起来不太好(截图)
那么发送xml数据还有其他想法吗? :x Thx
答案 0 :(得分:0)
您可以将XML作为文本发送给您,不是吗?
public interface ApiHttpPost {
@Headers({"Content-Type: application/xml; charset=utf-8"})
@POST("confidential")
Call<qosFeature> getqosFeature (@Header("Authorization") String authHeader, @Body String body);
}
答案 1 :(得分:0)
好的,我找到了解决问题的确切方法。
上述解决方案的问题是,将xml数据作为字符串类型发送,这是我不想要的。
所以我在另一个帖子上找到了这个答案,我应用它并且它起作用了 - &#39; - &#39; (创建类型为application / xml的对象)
此处主题为:{{3}}