改造:如何发出 XML 请求并取回 JSON 响应

Posted

技术标签:

【中文标题】改造:如何发出 XML 请求并取回 JSON 响应【英文标题】:Retrofit: How to make XML request and get back JSON response 【发布时间】:2018-09-16 21:27:45 【问题描述】:

我怎样才能发出一个简单的 text/xml POST 请求并使用改造 2 取回 JSON !!!???注 1:我已经知道如何制作 JSON GET/POST请求并返回 JSON 作为响应。注释 2:我有一个端点,其中请求采用 XML SOAP 格式,响应采用 JSON 格式。为澄清起见,我将在此处发布示例请求响应:

XML 示例请求:

<?xml version="1.0" encoding="utf-8"?>
<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">
  <soap12:Body>
    <Login xmlns="http://tempuri.org/">
      <username>0370079361</username>
      <password>4321</password>
    </Login>
  </soap12:Body>
</soap12:Envelope>

JSON 示例响应:


    "UserID": 2081,
    "FailureText": null,
    "UserValidPasswordCode": 2081,
    "UserPatientIsActiveWithNationalIDCode": true

【问题讨论】:

【参考方案1】:

自己找到了答案,其实超级简单。

APIService 类:(RequestBody 是这里的关键)

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

public interface RetrofitAPIService 
//    @Headers("Content-Type: application/json")
    @POST("/webservice.asmx?op=Login")
    Call<RetrofitResponseBody> login(@Body RequestBody body);

注意:以下代码的关键是这一行:

RequestBody requestBody = RequestBody.create(MediaType.parse("text/xml"), requestBodyText);

MainActivity.java:

 @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText userName = findViewById(R.id.userName);
        EditText password = findViewById(R.id.password);

        RetrofitAPIService mAPIService = RetrofitAPIUtils.getRetrofitAPIService();

        String requestBodyText = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\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\">\n" +
                "  <soap12:Body>\n" +
                "    <Login xmlns=\"http://tempuri.org/\">\n" +
                "      <username>" + userName.getText() + "</username>\n" +
                "      <password>" + password.getText() + "</password>\n" +
                "    </Login>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/xml"), requestBodyText);
        Call<RetrofitResponseBody> response = mAPIService.login(requestBody);

        response.enqueue(new Callback<RetrofitResponseBody>() 
            @Override
            public void onResponse(Call<RetrofitResponseBody> call, Response<RetrofitResponseBody> response) 
                try 
                    Log.d("JavadR:",
                            response.body().getUserID().toString() +
                                    " - " +
                                    response.body().getFailureText()
                    );
                 catch (Exception e) 
                    e.printStackTrace();
                
            

            @Override
            public void onFailure(Call<RetrofitResponseBody> call, Throwable t) 

            
        );

    

RetrofitResponseBody 并不太重要,但为了保持一致性和方便,我将在此处发布:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class RetrofitResponseBody 

    @SerializedName("UserID")
    @Expose
    private Integer userID;
    @SerializedName("FailureText")
    @Expose
    private Object failureText;
    @SerializedName("UserValidPasswordCode")
    @Expose
    private Integer userValidPasswordCode;
    @SerializedName("UserPatientIsActiveWithNationalIDCode")
    @Expose
    private Boolean userPatientIsActiveWithNationalIDCode;

    public Integer getUserID() 
        return userID;
    

    public void setUserID(Integer userID) 
        this.userID = userID;
    

    public Object getFailureText() 
        return failureText;
    

    public void setFailureText(Object failureText) 
        this.failureText = failureText;
    

    public Integer getUserValidPasswordCode() 
        return userValidPasswordCode;
    

    public void setUserValidPasswordCode(Integer userValidPasswordCode) 
        this.userValidPasswordCode = userValidPasswordCode;
    

    public Boolean getUserPatientIsActiveWithNationalIDCode() 
        return userPatientIsActiveWithNationalIDCode;
    

    public void setUserPatientIsActiveWithNationalIDCode(Boolean userPatientIsActiveWithNationalIDCode) 
        this.userPatientIsActiveWithNationalIDCode = userPatientIsActiveWithNationalIDCode;
    


【讨论】:

@Javed 如果响应也是与请求 xml 相同的 xml 格式,您可以发布答案吗?我遇到了 RetrofitResponseBody POJO 类的问题。它显示“org.simpleframework.xml.core.ConstructorException:参数'soap:Body'在com.kgk.kis.ui.auth.LoginResponse类中没有匹配”

以上是关于改造:如何发出 XML 请求并取回 JSON 响应的主要内容,如果未能解决你的问题,请参考以下文章

在进行改造 2 发布请求时,响应正文为空

如何通过改造发布数据并以正确的格式获得响应?

如何将改造 JSON 响应转换为列表?

向 Web 服务发出获取请求,获取 json 响应并在 Qt 中更新 GUI

ajax

发出跨域请求时无法使用 JSONP 取回任何数据