现在我想向文章节点添加一些数据。例如:
{"title":"Test","content":"Test text","keyWords":"1,2,3","date":"Aug 14, 2015 5:52:16 PM"}
Firebse REST API文档说,我应该POST数据,但是日志写入
服务器返回HTTP响应代码:400为URL: HTTPS:?//$TEST-BASE.firebaseio.com/articles.json AUTH = $ TOKEN
$TEST-BASE
和$TOKEN
是有效参数。我可以清楚地看到GET响应。怎么了?代码:
公共类JSONTest {
public static void main(String[] args) throws IOException {
post("{\"title\":\"Test\",\"content\":\"Test text\",\"keyWords\":\"1,2,3\",\"date\":\"Aug 14, 2015 5:52:16 PM\"}", "https://<TEST-BASE>.firebaseio.com/articles.json?auth=<TOKEN>");
}
private static void post(String json, String urlString) {
OutputStreamWriter out = null;
try {
URL url = new URL(urlString);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
out = new OutputStreamWriter(
httpCon.getOutputStream());
System.out.println(json);
out.write(json);
httpCon.getInputStream();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
答案 0 :(得分:1)
我希望我有足够的声望点才能留下评论= P,但我相信你获得400的原因是因为你没有将你的数据发送到正确的终点。尝试从网址中删除.json
:
https://<TEST-BASE>.firebaseio.com/articles?auth=<TOKEN>