Monday, August 1, 2011

Java Servlet problem

Hi,

I made an app. for Android which uses the C2DM service from Google. I
made a server simulator from some tutorials and it works fine. My
problem is, I tried to build a Java Servlet. From the Android device
it receives fine the message and saves the Registration ID, but when I
try to send a https POST request to the Google C2DM Server it always
gets a SocketTimeoutException : Timeout while fetching:
https://android.clients.google.com/c2dm/send.
I don't get why this is happening when the same works on the Android
device. Here is the code:

String auth_key = TOKEN; //The AuthToken from Google Client Login
StringBuilder postDataBuilder = new StringBuilder();
//some parameters to pass, I've checked and it's correct, it's working
with Fiddler
postDataBuilder.append(PARAM_REGISTRATION_ID).append("=").append(REGISTRATION_ID);
postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append("0");
postDataBuilder.append("&").append("data.payload").append("=").append(URLEncoder.encode(message,
UTF8));
byte[] postData = postDataBuilder.toString().getBytes(UTF8);
URL url = new URL("https://android.clients.google.com/c2dm/send");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);

conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-
urlencoded;charset=UTF-8");
conn.setRequestProperty("Content-
Length",Integer.toString(postData.length));
conn.setRequestProperty("Authorization", "GoogleLogin auth="+
auth_key);

OutputStream out = conn.getOutputStream();
out.write(postData);
out.close();

int responseCode = conn.getResponseCode();
//here comes the error processing, but I can't reach it, because of
the exception.
...
if (responseCode == 401 || responseCode == 403) {


Thanks for your help :).

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

No comments:

Post a Comment