0

The following REST-based Web Service in written in ASP .NET:

public class DeviceInfo {
    public string Model { get; set; }
    public string Serial { get; set; }
}

public class DeviceManagerController : ApiController  {
    ...
    public string Post([FromBody]DeviceInfo info) {
       ...
    }
}

Here is how I am building the JSON object in Android:

    URL url = new URL (url);
    HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    urlConn.setDoInput (true);
    urlConn.setDoOutput (true);
    urlConn.setUseCaches (false);
    urlConn.setRequestProperty("Content-Type","application/json");   
    urlConn.connect();

    // Send POST output.
    JSONObject jsonParam = new JSONObject();
    jsonParam.put("Model", "HP");
    jsonParam.put("Serial", "1234");

    DataOutputStream printout = new DataOutputStream(urlConn.getOutputStream ());
    String jsonStr = jsonParam.toString();
    String encodedStr = URLEncoder.encode(jsonStr,"UTF-8");
    printout.writeChars(encodedStr);
    printout.flush ();
    printout.close ();

This code successfully invokes the web service. However, the parameter (DeviceInfo) is null.

I even tried passing jsonStr directly instead of encoding them first. However, in this case, although the parameter info is no longer null, the members Model and Serial are still null.

I am wondering if there is something else that I missed. Regards.

Peter
  • 11,260
  • 14
  • 78
  • 155

2 Answers2

0

Using OutputStreamWriter instead of DataOutputStream takes care of the problem:

OutputStream os = urlConn.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(os, "UTF-8");
writer.write(jsonStr);
writer.close();
os.close();
Peter
  • 11,260
  • 14
  • 78
  • 155
-1

It will cost you more time and energy than it is necessary. Use some external json processing lib like Gson or Jackson. Create data model class, annotate properties with (JsonProperty annotation) and just generate proper json String. It may help you using them:

http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

When is the @JsonProperty property used and what is it used for?

Sebastian Pakieła
  • 2,970
  • 1
  • 16
  • 24
  • Thank you for your help. However, your answer is a bit misleading. JSONObject is provided with Android SDK so that you don't need to use external libraries. – Peter Jul 13 '15 at 15:59
  • Yes it is provided, but you should know that it is one of the worst way to do it. Imagine that your dto have 100 fields, arrays and you want to parse it to your object (containing for example hashmaps, arraylist and whatever you want) with this built in json handling mechanism. Now imagine the code of parser for it object. With using external library (btw Gson is Google developed library) it is always just creating data models, annotating and ALWAYS SINGLE LINE OF PARSING. Literally single line will convert json String to your object. – Sebastian Pakieła Jul 13 '15 at 16:51
  • Well using these libs is like wearing new shoes compared to walking on glass without them. Its possible but it's not good for you. And it hurts. I didn't recommended it without reason. – Sebastian Pakieła Jul 13 '15 at 16:52