通过更改以下代码使程序正常工作。与大多数情况下一样,使用JSON
库来JSON
像上面那样创建复合体是一个好习惯,而不是像大多数时候手动创建那样,手动创建复合体JSON
可能会带来很多麻烦。
import org.json.JSONObject;
public void callingGraph(){
CloseableHttpClient client= null;
CloseableHttpResponse response= null;
client= HttpClients.createDefault();
HttpPost httpPost= new HttpPost("https://api.github.com/graphql");
httpPost.addHeader("Authorization","Bearer myToken");
httpPost.addHeader("Accept","application/json");
JSONObject jsonObj = new JSONObject();
jsonobj.put("query", "{repository(owner: \"wso2-extensions\", name: \"identity-inbound-auth-oauth\") { object(expression: \"83253ce50f189db30c54f13afa5d99021e2d7ece\") { ... on Commit { blame(path: \"components/org.wso2.carbon.identity.oauth.endpoint/src/main/java/org/wso2/carbon/identity/oauth/endpoint/authz/OAuth2AuthzEndpoint.java\") { ranges { startingLine, endingLine, age, commit { message url history(first: 2) { edges { node { message, url } } } author { name, email } } } } } } } }");
try {
StringEntity entity= new StringEntity(jsonObj.toString());
httpPost.setEntity(entity);
response= client.execute(httpPost);
}
catch(UnsupportedEncodingException e){
e.printStackTrace();
}
catch(ClientProtocolException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
try{
BufferedReader reader= new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line= null;
StringBuilder builder= new StringBuilder();
while((line=reader.readLine())!= null){
builder.append(line);
}
System.out.println(builder.toString());
}
catch(Exception e){
e.printStackTrace();
}
}