[英]Android c2dm server simulator on emulator
I am following android c2dm example from following link: http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html 我正在通过以下链接关注android c2dm示例: http ://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html
I have implemented the client side successfully and have got my registration id. 我已经成功实现了客户端并获得了我的注册ID。 but i am having some issues in server end using the same example actually the issue is in getAuthentification method and i am getting following exception at HttpResponse response = client.execute(post). 但是我在使用相同示例的服务器端遇到一些问题,实际上问题出在getAuthentification方法中,并且我在HttpResponse response = client.execute(post)处遇到以下异常。
java.net.UnknownHostException: www.google.com java.net.UnknownHostException:www.google.com
Following is my code: 以下是我的代码:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"https://www.google.com/accounts/ClientLogin");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("Email","you....@gmail.com"));
nameValuePairs.add(new BasicNameValuePair("Passwd","*********"));
nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE"));
nameValuePairs.add(new BasicNameValuePair("source",
"Google-cURL-Example"));
nameValuePairs.add(new BasicNameValuePair("service", "ac2dm"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
Log.e("HttpResponse", line);
if (line.startsWith("Auth=")) {
Editor edit = prefManager.edit();
edit.putString(AUTH, line.substring(5));
edit.commit();
String s = prefManager.getString(AUTH, "n/a");
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
}
} catch (IOException e) {
e.printStackTrace();
}
Please help me? 请帮我? Your help would be highly appreciable. 您的帮助将非常可观。 Thanks, 谢谢,
I had this exact same issue last week. 上周我也遇到了同样的问题。 When the C2DM servers return a 302 Moved (www.google.com) what they ACTUALLY mean is the authentication failed. 当C2DM服务器返回302移动(www.google.com)时,它们的实际含义是认证失败。 The problem is almost certainly your authentication code, so re-check the code you're using to get the auth code from the ClientLogin API. 问题几乎可以肯定是您的身份验证代码,因此请重新检查用于从ClientLogin API获取身份验证代码的代码。 Note that the HTTP response contains a bunch of information, not just the auth code, so you do need to parse it correctly (that was my mistake). 请注意,HTTP响应包含大量信息,而不仅仅是auth代码,因此您需要正确解析它(这是我的错误)。
public static String getClientLoginAuthToken(String email, String password) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://www.google.com/accounts/ClientLogin");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("Email", email));
nameValuePairs.add(new BasicNameValuePair("Passwd", password));
nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE"));
nameValuePairs.add(new BasicNameValuePair("source","Google-cURL-Example"));
nameValuePairs.add(new BasicNameValuePair("service", "ac2dm"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
if (line.startsWith("Auth=")) {
return line.substring(5);
}
}
} catch (IOException e) {
e.printStackTrace();
}
Log.e(TAG, "Failed to get C2DM auth code");
return "";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.