繁体   English   中英

模拟器上的Android c2dm服务器模拟器

[英]Android c2dm server simulator on emulator

我正在通过以下链接关注android c2dm示例: http ://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html

我已经成功实现了客户端并获得了我的注册ID。 但是我在使用相同示例的服务器端遇到一些问题,实际上问题出在getAuthentification方法中,并且我在HttpResponse response = client.execute(post)处遇到以下异常。

java.net.UnknownHostException:www.google.com

以下是我的代码:

    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();
    }

请帮我? 您的帮助将非常可观。 谢谢,

上周我也遇到了同样的问题。 当C2DM服务器返回302移动(www.google.com)时,它们的实际含义是认证失败。 问题几乎可以肯定是您的身份验证代码,因此请重新检查用于从ClientLogin API获取身份验证代码的代码。 请注意,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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM