更新JSONArray中的元素使用HttpUrlConnection在Java中的PUT。

huangapple 未分类评论59阅读模式
英文:

Update JSONArray element usinh HttpUrlConnection PUT in java

问题

以下是翻译好的部分:

String POST_PARAMS = "field1=10";//working fine
//String POST_PARAMS2 = "field2={"key":"day","value":"Tuesday"}"; return 400 code in PUT action
URL obj = new URL("https://someurl");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("PUT");
con.setRequestProperty("User-Agent", "Mozilla/5.0");

con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();

Above code works but when I try to update field2 value using POST_PARAMS2. It returns me http response code 400. I want to update day value from Monday to Tuesday using POST_PARAMS2 commented above.

Any help or suggestion is appreciated!

英文:

Sample JSON is as below:

{
  "field1":9,
  "field2":[{\"key\":\"day\",\"value\":"Monday"},{\"key\":"month",\"value\":\"January\"}]
}

I am trying to update JSON field value using below code.

String POST_PARAMS = "field1=10";//working fine
//String POST_PARAMS2 = "field2={"key":"day","value":"Tuesday"}"; return 400 code in PUT action
URL obj = new URL("https://someurl");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("PUT");
con.setRequestProperty("User-Agent", "Mozilla/5.0");

con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();

Above code works but when I try to update field2 value using POST_PARAMS2. It returns me http response code 400. I want to update day value from Monday to Tuesday using POST_PARAMS2 commented above.

Any help or suggestion is appreciated!

huangapple
  • 本文由 发表于 2020年7月29日 00:16:07
  • 转载请务必保留本文链接:https://java.coder-hub.com/63138498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定