收藏
回答

java 调用 api 返回错误?

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]"},语句在开发控制平台测试没有问题。不知哪一步错误?

回答关注问题邀请回答
收藏

3 个回答

  • hbzyliyong
    hbzyliyong
    2020-04-24

    新手,都是网上看看,不过你的方法有效,谢谢

    2020-04-24
    有用
    回复
  • bugtao
    bugtao
    2020-04-24

    tongwen

    2020-04-24
    有用
    回复 1
    • bugtao
      bugtao
      2020-04-24
      6
      2020-04-24
      回复
  • Mr.Zhao
    Mr.Zhao
    2020-04-24

    会不会用RestTemplate,会的话我给你写个demo。 HttpURLConnection太恶心了

    2020-04-24
    有用
    回复 2
    • Mr.Zhao
      Mr.Zhao
      2020-04-24
      HttpURLConnection
      2020-04-24
      1
      回复
    • Mr.Zhao
      Mr.Zhao
      2020-04-24
      RestTemplate
      2020-04-24
      1
      回复
登录 后发表内容
问题标签