I usually use $.ajax to send my application data to my WebService, as following:
$.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. 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. And it doesn't work with JAVA Code.
Question: How to port a $.ajax POST like mine above, as a 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. It must by library drivers on how handle data.
My JAVA application sends data as a single JSON object as String.
However, in some weird way, my WebService receives data from my WebApplication JS as a structured JSON object as is.
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.