【发布时间】:2015-09-21 16:40:19
【问题描述】:
我想在 Android 中发送一个简单的 POST 请求,其正文等于:[{ "value": ["test"]}]
由于请求非常简单,我尝试使用以下代码:
try {
URL url = new URL("******");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
//headers (all of them are simple strings)
connection.setRequestMethod("POST");
connection.setRequestProperty("X-OAPI-Key", "123****");
connection.setRequestProperty("X-ISS-Key", "*******");
connection.setRequestProperty("Content-Type", "application/json");
//body that I want to send
String urlParameters = "[{\"value\":[test]}]";
// Send post request
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
你能帮我解决这个问题吗?我不知道如何才能准确地以这种形式发送正文?
谢谢!
【问题讨论】:
-
你想以
JSONObject发送数据吗? -
尝试使用 Volley 库。
-
在 Volley 中只需使用 POST 调用 StringRequest 就可以了!