0
votes

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

1
could you tell us what/where the error is? (line, which piece of code etc.) - roarster
Error 500 Internal server error, I get that reading the 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
Look at this. It's great library for create rest client in Java: square.github.io/retrofit - Aleksander Mielczarek

1 Answers

0
votes

Well, I remembered (thanks to a friend) I have wireshark so I checked what the server was receiving:

With writeUTF() add '/000]' before the JSON. With writeChars() add '.' after (or before) every char.

I change and write like a byte[] as I had before (and how most of sites I saw did). So:

With write(data.toString().getBytes()) and it works.

Now, I try to figure out why it didn't work initially... Probably the server had mistakes too...

Anyway, thanks to people who has commented.