The objective is to sen开发者_JAVA技巧d data over an HTTP Post with JSON in it. The C# Source does like this:
Http.AddFileField("file", "file.text", ms);
String json = JsonConvert.SerializeObject(d, Formatting.None, jSettings);
IOUtil.WriteStringToStream(json, ms);
ms.Position = 0;
How to do this on Android?
Try this -
HttpURLConnection urlConn = null;
URL mUrl = new URL(url);
urlConn = (HttpURLConnection) mUrl.openConnection();
urlConn.setRequestMethod("POST");
urlConn.addRequestProperty("Content-Type", "application/" + "json");
urlConn.setDoOutput(true);
//query is your json string
if (query != null)
{
urlConn.setRequestProperty("Content-Length", Integer.toString(query.length()));
urlConn.getOutputStream().write(query.getBytes("UTF8"));
}
urlConn.connect();
精彩评论