String generalUrl="https://api.weixin.qq.com/tcb/databaseadd?access_token="+tocken;
try {
URL url = new URL(generalUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("accept","application/json");
ccs="{\"env\": \"clound-zygj\",\"query\": \"db.collection(\"zg_gzb\").add({data: [{\"employeenumber\":\"Z01117\"}]})\"}";
System.out.println(ccs);
byte[] writebytes = ccs.getBytes();
conn.setRequestProperty("Content-Length", String.valueOf(writebytes.length));
OutputStream outwritestream = conn.getOutputStream();
outwritestream.write(writebytes);
outwritestream.flush();
outwritestream.close();
if (200 == conn.getResponseCode()){
is = conn.getInputStream();
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line;
while ((line = br.readLine()) != null){
result.append(line);
}
}else{
System.out.println("ResponseCode is an error code:" + conn.getResponseCode());
}
}catch (MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}finally {
try{
if(br != null){
br.close();
}
if(is != null){
is.close();
}
}catch (IOException ioe){
ioe.printStackTrace();
}
conn.disconnect();
}
System.out.println(result.toString());
{"errcode":47001,"errmsg":"data format error hint: [5eLBH.wgE-hUGbXa]"},语句在开发控制平台测试没有问题。不知哪一步错误?
新手,都是网上看看,不过你的方法有效,谢谢
tongwen
会不会用RestTemplate,会的话我给你写个demo。 HttpURLConnection太恶心了
String uri = String.format("https://api.weixin.qq.com/tcb/databaseadd?access_token=%s", wxUtil.getAccessToken());
URL url = new URL(uri);
JSONObject obj = new JSONObject();
obj.put("env", "binnie-37s7d");
obj.put("query", "db.collection('todos')\n" +
" .add({\n" +
" data: {\n" +
" _id: 'apple-1',\n" +
" name: 'apple',\n" +
" category: 'fruit',\n" +
" price: 10,\n" +
" }\n" +
" })\n");
String json = obj.toJSONString();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json");
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(json);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String lines;
StringBuilder sb = new StringBuilder("");
while ((lines = reader.readLine()) != null) {
sb.append(lines);
}
reader.close();
connection.disconnect();
System.out.println(sb.toString());
String uri = String.format("https://api.weixin.qq.com/tcb/databaseadd?access_token=%s", wxUtil.getAccessToken());
JSONObject obj = new JSONObject();
obj.put("env", "binnie-37s7d");
obj.put("query", "db.collection('todos')\n" +
" .add({\n" +
" data: {\n" +
" _id: 'apple-2',\n" +
" name: 'apple',\n" +
" category: 'fruit',\n" +
" price: 10,\n" +
" }\n" +
" })\n");
String json = obj.toJSONString();
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.postForEntity(uri, json, String.class);
•
String body = responseEntity.getBody();
System.out.println(body);