在 android 中将 Header 和 BasicHeader 与 HttpUrlConnection 结合使用

Using Header and BasicHeader with HttpUrlConnection in android

在 android 6.0 中,org.apache.http.legacy 库已弃用,因此我尝试迁移一个项目以使用 HttpURLConnection。 在项目中,我使用了HeaderBasicHeader。我没有找到如何用 HttpUrlConnection 替换这些 类。我只想知道如何使用 HttpUrlConnection.

在请求中添加 header
  URL url=new URL("YOURURL");
  HttpURLConnection connection= (HttpURLConnection) url.openConnection();//establishing connection from url
  connection.setDoOutput(true);
  connection.setRequestMethod("POST");//method type
  connection.setRequestProperty("Content-Type","application/json");// setting headers