I have a webservice (rest in c#) and I want to insert data using JSON with an Android App (Eclipse, java).
Here is the relevant java code:
// data
JSONObject data = new JSONObject();
data.put("param1", myObject.getParam1());
data.put("param2", myObject.getParam2());
data.put("param3", myObject.getParam3());
data.put("param4", myObject.getParam4());
Log.i("dmc", "data: "+data.toString());
// http connection
conn = (HttpURLConnection) url.openConnection();
Log.i("dmc","HttpURLConnection: "+ conn.equals(null));
conn.setReadTimeout( 30000 /*milliseconds*/ );
conn.setConnectTimeout( 45000 /* milliseconds */ );
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("content-type", "application/json");
conn.connect();
out = new DataOutputStream(conn.getOutputStream());
out.writeUTF(data.toString());
Log.i("dmc", data.toString());
out.flush();`
This is the last I tried, I've encode it and the same.
I'm not sure what I should post about the server but:
context.MapRoute(
"Description",
"the/path/{id}",
new
{
controller = "myController",
action = "MyAction",
id = UrlParameter.Optional
}
);
Controller action:
public JsonResult MyAction(int? id, myObject item)
{
switch (Request.HttpMethod)
{
case "POST":
return Json(myObjectManager.InsertMyObject(item));
case "PUT":
return Json(myObjectManager.UpdateMyObject(item));
case "GET":
return Json(myObjectManager.GetMyObject(id.GetValueOrDefault()),
JsonRequestBehavior.AllowGet);
case "DELETE":
return Json(myObjectManager.DeleteMyObject(id.GetValueOrDefault()));
}
return Json(new { Error = true, Message = "Unknown HTTP op" });
}
EDIT: I get error 500 - Internal server error.
Invalid JSON primitive. The first three lines of the trace are:
Argument Exception: Invalid JSON primitive System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() +745 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +362
System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) +115
conn.getInputStream()(ErrorStream actually), but I don't get the exact line. i'll edit the question with first lines of the error trace. - Frs