英文:
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!
专注分享java语言的经验与见解,让所有开发者获益!
评论