开发者

How to do this in Android? (C# Source)

开发者 https://www.devze.com 2023-03-22 14:42 出处:网络
The objective is to sen开发者_JAVA技巧d data over an HTTP Post with JSON in it. The C# Source does like this:

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消