Skip to content

Instantly share code, notes, and snippets.

@bgloh
Last active December 31, 2021 03:48
Show Gist options
  • Save bgloh/e1e01393a7dcd5a0f317d2f1c18884a4 to your computer and use it in GitHub Desktop.
Save bgloh/e1e01393a7dcd5a0f317d2f1c18884a4 to your computer and use it in GitHub Desktop.
http post for minecraft java
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public void whenAsynchronousPostRequest_thenCorrect(CommandSender sender) {
JSONObject json = new JSONObject();
JSONObjec child = new JSONObjec();
json.put("PK", "PLAYER");
json.put("SK", "playerID#abc@gmail.com");
json.put("playerID", "abc@gmail.com");
// child json
child.put("age",10);
child.put("level",0);
//
json.put("playerInfo", child);
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(json.toString(), JSON);
Request request = new Request.Builder()
.url( "lungrow-minecraft-create-data")
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
sender.sendMessage(ChatColor.RED + "http failure" + e);
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
} else {
// do something wih the result
ResponseBody responseBody = response.body();
sender.sendMessage(ChatColor.GREEN + "http success");
sender.sendMessage(ChatColor.GREEN + "response:" + responseBody.string());
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment