【发布时间】:2017-01-24 12:48:28
【问题描述】:
我想使用我的活动中的 json 对象数据和标头向 Firebase url 发送请求,
我已经检查了来自 rest 客户端的 url,我在那里成功,我收到了正确的响应,但是当我从 android 调用它时,我无法得到响应:
这是我的代码:
public static void pushFCMNotification(String userDeviceIdKey) throws Exception{
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/json");
conn.setRequestProperty("Authorization","key="+authKey);
Log.e("authkey==> ",authKey+"");
JSONObject json = new JSONObject();
json.put("to",userDeviceIdKey.trim());
Log.e("deviceidkey==> ",userDeviceIdKey.trim()+"");
JSONObject info = new JSONObject();
info.put("title", "Notificatoin Title"); // Notification title
info.put("body", "Hello Test notification"); // Notification body
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
}
这是我想用 url 发送的 json 结构
我没有例外,如果这个请求成功,我应该在我的设备上收到一个通知,这是从 rest 客户端而不是从 android,
请检查我在哪里犯了错误以及如何检查我的请求的响应
【问题讨论】:
标签: android json httpurlconnection