Thursday, December 23, 2010

[android-developers] c2DM error when third-party server sends data to push

I am trying to push data from my server hosting a webservice but I get
the following error:

<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>411 Length Required</title>
<style><!--
body {font-family: arial,sans-serif}
div.nav {margin-top: 1ex}
div.nav A {font-size: 10pt; font-family: arial,sans-serif}
span.nav {font-size: 10pt; font-family: arial,sans-serif; font-weight:
bold}
div.nav A,span.big {font-size: 12pt; color: #0000cc}
div.nav A {font-size: 10pt; color: black}
A.l:link {color: #6f6f6f}
A.u:link {color: green}
//--></style>
<script><!--
var rc=411;
//-->
</script>
</head>
<body text=#000000 bgcolor=#ffffff>
<table border=0 cellpadding=2 cellspacing=0 width=100%><tr><td
rowspan=3 width=1% nowrap>
<b><font face=times color=#0039b6 size=10>G</font><font face=times
color=#c41200 size=10>o</font><font face=times color=#f3c518
size=10>o</font><font face=times color=#0039b6 size=10>g</font><font
face=times color=#30a72f size=10>l</font><font face=times
color=#c41200 size=10>e</font>&nbsp;&nbsp;</b>
<td>&nbsp;</td></tr>
<tr><td bgcolor="#3366cc"><font face=arial,sans-serif
color="#ffffff"><b>Error</b></td></tr>
<tr><td>&nbsp;</td></tr></table>
<blockquote>
<H1>Length Required</H1>
POST requests require a <code>Content-length</code> header.

<p>
</blockquote>
<table width=100% cellpadding=0 cellspacing=0><tr><td
bgcolor="#3366cc"><img alt="" width=1 height=4></td></tr></table>
</body></html>


Here is the java code that posts message to google c2dm which inturn
will send a push notification to the device:


private void postData(String auth, String id, String data) {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"https://android.apis.google.com/c2dm/send");
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("registration_id", id));
formparams.add(new BasicNameValuePair("data.payload", data));
formparams.add(new BasicNameValuePair("collapse_key", "something"));
//formparams.add(new BasicNameValuePair("Content-length",
""+data.length()));

if (null != id) {
formparams.add(new BasicNameValuePair("id", id));
}
UrlEncodedFormEntity entity = null;
try {
entity = new UrlEncodedFormEntity(formparams, "UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
httppost.setEntity(entity);
Header header = new BasicHeader("Authorization: GoogleLogin auth=",
auth);
Header header2 = new BasicHeader("Content-length=",
""+data.length());
httppost.addHeader(header);
httppost.addHeader(header2);
httppost.getParams().setParameter("Content-length:",
""+data.length());
HttpContext localContext = new BasicHttpContext();
HttpResponse response = null;
try {
response = client.execute(httppost, localContext);
HttpEntity entity2 = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
Header[] headers = response.getAllHeaders();
for (int i = 0; i < headers.length; i++) {
System.out.println(headers[i]);
}
System.out.println("----------------------------------------");

if (response.getEntity() != null) {
System.out.println(EntityUtils.toString(response.getEntity()));
}

} catch (Exception e) {
System.err.println(e);
} finally {
}
}

Note:
1. I successfully got the registration id from the client (device)
2. I successfully get the "Auth" from an earlier post on the server
side

Any help will be greatly appreciated

Thank you
-Nagaraj

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment