简体   繁体   English

Java Http POST ajax等效于发送结构化数据JSON

[英]Java Http POST ajax equivalent for sending structured data JSON

I usually use $.ajax to send my application data to my WebService, as following: 我通常使用$ .ajax将应用程序数据发送到WebService,如下所示:

$.ajax({
            type: "POST",
            url: server + "/fosco/set", 
            data: {   
                "page":"0",
                "requestType":"DataSet",
                "dataset":createClientJSONObject(),
                "params":"[]"
            },
            headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'User-Id' : localStorage.getItem("USER_ID")
            },
            success: SuccessSetData

Now, I'm using the same WebService for my Java Application, so I wonder the equivalent code to send it as Java Application, using HTTP POST. 现在,我为Java应用程序使用了相同的WebService,所以我想知道使用HTTP POST将其作为Java应用程序发送的等效代码。 I started something like following, but don't work: 我开始如下操作,但不起作用:

String url = "http://localhost/service/index.php/fosco/set";
        String returnBody = "";
        Gson gson = new Gson();
        Client fc = new Client();
        try {
            HttpRequest httpRequest = new HttpRequest();
            Hashtable<String, String> params = new Hashtable<String, String>();
            params.put("page", "0");
            params.put("requestType", "DataSet");
            params.put("dataset", gson.toJson(new Client()));
            params.put("params", "[]");
            returnBody = httpRequest.post(url,
                    params);

I have debugged my PHP WebService and it show different datasets, it shows JAVA Application dataset as one entire String, but $.ajax as an structured JSON Object. 我已经调试了PHP WebService,它显示了不同的数据集,将JAVA Application数据集显示为一个完整的String,但是将$ .ajax显示为结构化的JSON对象。 And it doesn't work with JAVA Code. 而且它不适用于JAVA代码。

Question: How to port a $.ajax POST like mine above, as a Java Http POST? 问题:如何将像我上面的$ .ajax POST移植为Java Http POST?

And this is how I figured out... 这就是我的想法

First, Debuging server-side application with var_dump, I saw that my Java application was sending a single string with entire JSON on it, in some different way to my Web Application (JavaScript) one. 首先,使用var_dump调试服务器端应用程序,我看到Java应用程序正在发送带有完整JSON的单个字符串,其方式与Web应用程序(JavaScript)不同。 It must by library drivers on how handle data. 它必须由库驱动程序来处理数据。

My JAVA application sends data as a single JSON object as String. 我的JAVA应用程序将数据作为单个JSON对象作为String发送。

However, in some weird way, my WebService receives data from my WebApplication JS as a structured JSON object as is. 但是,以某种奇怪的方式,我的WebService从WebApplication JS接收的数据仍然是结构化JSON对象。

Then, I used a json_decode(String ,true) PHP function on my Web Service side to decode JAVA string as JSON object, to be recognized as it should. 然后,我在Web服务端使用了json_decode(String,true)PHP函数将JAVA字符串解码为JSON对象,以使其应被识别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM