Reputation: 223
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
}
Upvotes: 5
Views: 5760
Reputation: 223
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;
}
}
Upvotes: 7