我搜索了很多,但我找不到解决此问题的方法。
你可以帮我吗?Enviando POST para o GCM 回复代码:400 java.io.IOException:服务器返回HTTP响应代码:400为URL:https://gcm-http.googleapis.com/gcm/send at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) 在sun.net.www.protocol.http.HttpURLConnection $ 10.run(HttpURLConnection.java:1890) 在sun.net.www.protocol.http.HttpURLConnection $ 10.run(HttpURLConnection.java:1885) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1884) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1457) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) at br.com.farmas.util.Post2GCM.post(Post2GCM.java:46) 在br.com.farmas.main.AppMain.main(AppMain.java:17) 引起:java.io.IOException:服务器返回HTTP响应代码:400为URL:https://gcm-http.googleapis.com/gcm/send at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441) 在java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338) 在br.com.farmas.util.Post2GCM.post(Post2GCM.java:43) ......还有1个
下面,我展示了我正在执行的代码......
第43行如下:int responseCode = connection.getResponseCode();
Enviando POST para o GCM
Response Code: 400
java.io.IOException: Server returned HTTP response code: 400 for URL: https://gcm-http.googleapis.com/gcm/send
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1890)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1885)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1884)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1457)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at br.com.farmas.util.Post2GCM.post(Post2GCM.java:46)
at br.com.farmas.main.AppMain.main(AppMain.java:17)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://gcm-http.googleapis.com/gcm/send
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
at br.com.farmas.util.Post2GCM.post(Post2GCM.java:43)
... 1 more
以下是我拨打电话的第一段代码
public static void main(String[] args)
{
System.out.println("Enviando POST para o GCM");
Post2GCM.post(API_KEY, criarContent());
}
private static Content criarContent()
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Content content = new Content();
content.addRegId("eCvEu9f2LFM:APA91bEB7AnrTxP1owl_QrFTLUc35vxs2rGOCpyajU5RTsuPY427IhFF8EayEC3WThQ54ag7q6mwbKUywohGvK_q-RTH0QlglJjVqzH9b9X-gx6Nli9_RJlLRnwyV10iervfexFB3fOy");
content.addData("titulo", "Titulo Teste");
content.addData("message", "Message Teste");
content.addData("data", dateFormat.format(new Date()));
content.addData("id", "2");
return content;
}
以下是发生异常的第二个代码。异常发生在以下行:int responseCode = connection.getResponseCode();
public static void post(String apiKey, Content content)
{
try
{
URL url = new URL("https://gcm-http.googleapis.com/gcm/send");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "key=" + apiKey);
connection.setDoOutput(true);
ObjectMapper mapper = new ObjectMapper();
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
mapper.writeValue(outputStream, content);
outputStream.flush();
outputStream.close();
int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String linha = "";
StringBuffer resultado = new StringBuffer();
while ((linha = bufferedReader.readLine()) != null)
{
resultado.append(linha);
}
bufferedReader.close();
System.out.println(resultado);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}