GCM推送通知的http 411错误

时间:2015-12-24 11:08:14

标签: java android

我正试图点击gcm云来获取通知

我创建了web restfull web服务我实现了以下代码,但是我得到了以下错误

String gurl = "https://android.googleapis.com/gcm/send";
URL url = new URL(gurl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");

con.setRequestProperty("Authorization", "key=" + apikey);
con.setRequestProperty("registration_ids",regid);
con.setRequestProperty("data", "message="+msg);
con.setRequestProperty("Content-Length", "0");

con.setDoOutput(true);


int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(
        new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();

// 7. Print result
System.out.println(response.toString());
//return response.toString();
return response.toString();

我收到错误

VERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
java.io.IOException: Server returned HTTP response code: 411 for URL: https://android.googleapis.com/gcm/send
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1676)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1674)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1672)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1245)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at com.madhu.pack.SimpleWS.chattest1(SimpleWS.java:250)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at 

等等......

我是从Android客户端发布数据的。那么你们可以帮助我摆脱这个问题吗?

1 个答案:

答案 0 :(得分:0)

错误代码411代表“需要长度”。

您需要更改行

void Main()
{
    var bike = Factory.GetInstance<CheapBike>(() => new Information()
    {
        Name = "BMX",
        Description = "..."
    });

    //or
    var bike = Factory.GetInstance<CheapBike>(GetInfo);
}

private Information GetInfo()
{
     //do some logic to populate the info object to be passed
     //into the constructor during factory initialization
     return yourPopulatedInfoObject;
}

public static class Factory 
{
    public static T GetInstance<T>(Func<Information> func) where T : EntityBase
    {
        return (T)Activator.CreateInstance(typeof(T), func());
    }
}

而不是0,您必须提交数据的长度。