如何使用HttpUrl连接在android api 23中使用post发送名称-值对对象或内容-值对象?
//这是我试过的。 //我不知道如何附加inputurl和内容值,并使用HttpURLConnection将其发送到服务器 公共JSONObject getJsonFromUrl(字符串inputurl,ContentValues){ ///请在此处插入代码,以便在url中附加值。 connection.connect()如何使用HttpUrl连接在android api 23中使用post发送名称-值对对象或内容-值对象?,android,url,httpurlconnection,Android,Url,Httpurlconnection,//这是我试过的。 //我不知道如何附加inputurl和内容值,并使用HttpURLConnection将其发送到服务器 公共JSONObject getJsonFromUrl(字符串inputurl,ContentValues){ ///请在此处插入代码,以便在url中附加值。 connection.connect() }如果您想通过HttpUrlConnection实例将JSON对象发布到url,您可以编写如下示例 DataOutputStream wr = new DataOut
}如果您想通过HttpUrlConnection实例将JSON对象发布到url,您可以编写如下示例
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes(jsonObject.toString());
wr.flush();
wr.close();
Where,
jsonObject is the json object which you would be posting to the URL
整个代码可能如下所示
URL url = new URL("this would be your url where you want to POST");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
// when you are posting do make sure you assign appropriate header
// In this case POST.
httpURLConnection.setRequestMethod("POST");
httpURLConnection.connect();
// like this you can create your JOSN object which you want to send
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("email", "dddd@gmail.com");
jsonObject.addProperty("password", "password");
// And this is how you will write to the URL
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes(jsonObject.toString());
wr.flush();
wr.close();
Log.d("TAG", "" + IOUtils.toString(httpURLConnection.getInputStream()));
可能的重复可能的重复
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes(jsonObject.toString());
wr.flush();
wr.close();
Where,
jsonObject is the json object which you would be posting to the URL
URL url = new URL("this would be your url where you want to POST");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
// when you are posting do make sure you assign appropriate header
// In this case POST.
httpURLConnection.setRequestMethod("POST");
httpURLConnection.connect();
// like this you can create your JOSN object which you want to send
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("email", "dddd@gmail.com");
jsonObject.addProperty("password", "password");
// And this is how you will write to the URL
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes(jsonObject.toString());
wr.flush();
wr.close();
Log.d("TAG", "" + IOUtils.toString(httpURLConnection.getInputStream()));