Question

I was working on my android project with my friend and we stucked at some point. I'am trying to make an async post to WCF rest web service and on the service side, I'm inserting the data into db. I'm sending the data via JSON object. I open a dialogue box and when the request is done successful, dialogue box disappears. Now my problem is, I cannot make post request, actually, I don't get any error but it seems there is a problem. There is no problem on get request. I'm going to crazy, so I need your help. Here my codes

Thank you

JAVA

        progressDialog = ProgressDialog.show(Activity3.this, "Please wait ...",  "Task in progress ...", true);
        progressDialog.setCancelable(true);
        jarray = new JSONArray();
        json2 = new JSONObject();

        AsyncHttpClient client = new AsyncHttpClient();
        try {

            json2.put("CreateDate", "30.03.2014 15:30:00");
            json2.put("EventCategory", "Yemek");
            json2.put("EventID", "6");
            json2.put("EventName", "Kanatçı Haydar");
            json2.put("EventStatus", "A");
            json2.put("FsqID", "561239");
            json2.put("IsPublic", "False");
            json2.put("LastUpdate", "01.01.0001 00:00:00");
            json2.put("Quota", "8");
            json2.put("UserID", "42");

            StringEntity entity = new StringEntity(json2.toString()); 

            client.post(arg0.getContext(), PURL, entity, "application/json",
                    new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(String response) {
                    progressDialog.dismiss();
                    Toast.makeText(Activity3.this,response, Toast.LENGTH_LONG).show();
                }

            });

        }

WCF Service

This is how I handle POST request

[OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "New")]
    bool SetAllEvents(Stream st);

This function calls JSON parser

public bool SetAllEvents(Stream s)
    {
        SetEvents se = new SetEvents();

        var data = se.SetNewEvent(s, connStr);
        return true;
    }

This is how I parse JSON

StreamReader reader = new StreamReader(inputStream);
            string json = reader.ReadToEnd();

            var Jsonobject = JsonConvert.DeserializeObject<Events>(json);

            string eventName = Jsonobject.EventName;
Was it helpful?

Solution

What does your httppost header for the client side look like? The following may help you:

httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top