Retrofit: How to make XML request and get back JSON response

Javad

how could I make a simple text/xml POST request and get JSON back using retrofit 2 !!!???
note 1:i already know how to make JSON GET/POST request and get back JSON as a response.
note 2: I have an endpoint in which the request is in XML SOAP format and the response is in JSON format. for clarification I'll post sample request response here:

XML Sample Request:

<?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 Sample Response:

{
    "UserID": 2081,
    "FailureText": null,
    "UserValidPasswordCode": 2081,
    "UserPatientIsActiveWithNationalIDCode": true
}
Javad

found the answer myself, actually it was SUPER EASY.

APIService Class:(RequestBody is the key here)

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


note: the key for the following code is this line:

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

            }
        });

    }

the RetrofitResponseBody is not of too much importance but for consistency and convenience I'll post it here:

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

}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to send GET Request with JSON in Header in Retrofit

分類Dev

How do I get "nameValuePairs" in a JSON request using Retrofit?

分類Dev

How do I make a GET request with JSON in the request body

分類Dev

How to make httpd response 200 for options request?

分類Dev

How to get the raw JSON response of a HTTP request from `driver.page_source` in Selenium webdriver Firefox

分類Dev

How to compare XML response with Json in Karate

分類Dev

Alamofire Get Request&JSON Response(Yelp API)

分類Dev

how to get valid json response from server

分類Dev

How to get Typeahead with Bloodhound to work with a JSON response?

分類Dev

Parse JSON array response using Retrofit & Gson

分類Dev

Issue in getting json response data using Retrofit

分類Dev

Moshi + Retrofit - Handling JSON response of unknown type

分類Dev

Retrofit 2 - get error-object JSON (few POJO for same request)

分類Dev

Retrofit 2 - get error-object JSON (few POJO for same request)

分類Dev

How to use GET request to obtain JSON

分類Dev

how to get value from raw or xml HTML response by groovy

分類Dev

How to parse an error response with Retrofit2?

分類Dev

How to make ajax get/post request in express server?

分類Dev

How to make a "simple" GET request if the resource doesn't allow CORS?

分類Dev

How to make a get request to retrieve the latest document of an index in the elasticsearch cluster?

分類Dev

How can I send a simple GET to a URL and see response back in Lambda?

分類Dev

flask - how to get parameters from a JSON GET request

分類Dev

How to handle Dynamic JSON in Retrofit?

分類Dev

How to send request body in retrofit call?

分類Dev

How to get Json response from server in Intent Service in android?

分類Dev

How to get JSON response array all index values?

分類Dev

How to send an Array by Response/Request?

分類Dev

NodeJS Express Make Get Request

分類Dev

Response from Subscribe of async get request to be used in another get request

Related 関連記事

  1. 1

    How to send GET Request with JSON in Header in Retrofit

  2. 2

    How do I get "nameValuePairs" in a JSON request using Retrofit?

  3. 3

    How do I make a GET request with JSON in the request body

  4. 4

    How to make httpd response 200 for options request?

  5. 5

    How to get the raw JSON response of a HTTP request from `driver.page_source` in Selenium webdriver Firefox

  6. 6

    How to compare XML response with Json in Karate

  7. 7

    Alamofire Get Request&JSON Response(Yelp API)

  8. 8

    how to get valid json response from server

  9. 9

    How to get Typeahead with Bloodhound to work with a JSON response?

  10. 10

    Parse JSON array response using Retrofit & Gson

  11. 11

    Issue in getting json response data using Retrofit

  12. 12

    Moshi + Retrofit - Handling JSON response of unknown type

  13. 13

    Retrofit 2 - get error-object JSON (few POJO for same request)

  14. 14

    Retrofit 2 - get error-object JSON (few POJO for same request)

  15. 15

    How to use GET request to obtain JSON

  16. 16

    how to get value from raw or xml HTML response by groovy

  17. 17

    How to parse an error response with Retrofit2?

  18. 18

    How to make ajax get/post request in express server?

  19. 19

    How to make a "simple" GET request if the resource doesn't allow CORS?

  20. 20

    How to make a get request to retrieve the latest document of an index in the elasticsearch cluster?

  21. 21

    How can I send a simple GET to a URL and see response back in Lambda?

  22. 22

    flask - how to get parameters from a JSON GET request

  23. 23

    How to handle Dynamic JSON in Retrofit?

  24. 24

    How to send request body in retrofit call?

  25. 25

    How to get Json response from server in Intent Service in android?

  26. 26

    How to get JSON response array all index values?

  27. 27

    How to send an Array by Response/Request?

  28. 28

    NodeJS Express Make Get Request

  29. 29

    Response from Subscribe of async get request to be used in another get request

ホットタグ

アーカイブ