Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 改造:如何发出XML请求并返回JSON响应_Android_Json_Xml_Xmlhttprequest_Retrofit2 - Fatal编程技术网

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

Android 改造:如何发出XML请求并返回JSON响应,android,json,xml,xmlhttprequest,retrofit2,Android,Json,Xml,Xmlhttprequest,Retrofit2,我如何使用Reformation 2发出一个简单的文本/xml POST请求并获取JSON 注1:我已经知道如何发出JSON GET/POST请求并将JSON作为响应返回。 注2:我有一个端点,其中请求是XML SOAP格式,响应是JSON格式。为了澄清,我将在此处发布请求响应示例: XML示例请求: 我自己找到了答案,其实非常简单。 APIService类:(RequestBody是这里的键) MainActivity.java: @Override protected void o

我如何使用Reformation 2发出一个简单的文本/xml POST请求并获取JSON
注1:我已经知道如何发出JSON GET/POST请求并将JSON作为响应返回。
注2:我有一个端点,其中请求是XML SOAP格式,响应是JSON格式。为了澄清,我将在此处发布请求响应示例:

XML示例请求:


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

APIService类:(RequestBody是这里的键)

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

            }
        });

    }
{
    "UserID": 2081,
    "FailureText": null,
    "UserValidPasswordCode": 2081,
    "UserPatientIsActiveWithNationalIDCode": true
}
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);
 @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) {

            }
        });

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

}