android - 改造 : How to make XML request and get back JSON response

标签 android json xml xmlhttprequest retrofit2

我如何发出一个简单的文本/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
}

最佳答案

自己找到了答案,其实非常简单。

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);

主要 Activity .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;
    }

}

关于android - 改造 : How to make XML request and get back JSON response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49699776/

相关文章:

android - ImageView 的默认 ScaleType 是什么?

android - 如何在 Android 中为 GCM 项目创建服务器

android - 打盹模式和前台服务

java - 如何在 Java 中将 JSON 转换为 Base64 字符串?

java - JAXBContext 解码具有多个 rootElement 的 XML 字符串

android - 通过wifi访问Android设备中的本地主机网站

javascript - 使用 JavaScript 在 HTML 文件之间共享 JSON 对象

JSONArray 将响应 null 元素转换为 "null"

xml - 仅解析 xml 文件中的下一个子级别类别

xml - 错误 : XML validator says conetent not allowed in trailing section and other namespace issues?