我正在尝试创建一个到 Google 服务器的 HTTP POST 来获取 ClientLogin Auth(如所述here http://code.google.com/android/c2dm/index.html#push)。这篇文章的源代码并不是一个真正的谜,我找到了它here http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html#c2dm_sendmessage。 (我确信我的帖子有效,因为使用 CURL 我获得了身份验证)
该方法非常简单,我已经相应地修改了值,但是,当我执行以下行时,出现以下异常:
HttpResponse response = client.execute(post);
06-17 13:44:05.645: WARN/System.err(768): java.net.UnknownHostException: www.google.com
06-17 13:44:05.645: WARN/System.err(768): at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
06-17 13:44:05.645: WARN/System.err(768): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
06-17 13:44:05.645: WARN/System.err(768): at java.net.InetAddress.getAllByName(InetAddress.java:256)
06-17 13:44:05.645: WARN/System.err(768): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
06-17 13:44:05.645: WARN/System.err(768): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-17 13:44:05.645: WARN/System.err(768): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-17 13:44:05.645: WARN/System.err(768): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
06-17 13:44:05.645: WARN/System.err(768): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-17 13:44:05.645: WARN/System.err(768): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-17 13:44:05.645: WARN/System.err(768): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
etc.
etc.
etc…
(省略无关日志)。
在尝试了各种代码组合(在 google 和此处找到)、不同的 URL(带或不带 HTTPS)、常规 HTTP 等之后。我决定尝试以下操作:
try {
InetAddress address = InetAddress.getByName("http://www.google.com");
} catch (UnknownHostException e) {
e.printStackTrace();
}
我得到同样的错误:(也修剪了不相关的部分)
06-17 13:53:07.445: WARN/System.err(820): java.net.UnknownHostException: http://www.google.com
06-17 13:53:07.449: WARN/System.err(820): at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
06-17 13:53:07.449: WARN/System.err(820): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
06-17 13:53:07.449: WARN/System.err(820): at java.net.InetAddress.getByName(InetAddress.java:325)
现在我确定重新启动了 Eclipse、The Phone,我什至忘记了 Wi-Fi 并重新创建了它。我在手机上使用静态 IP 地址和 Google DNS 或 OpenDNS(我已经交换了它们来尝试)。手机上的互联网可以使用,其他应用程序也可以使用。
I do have
<uses-permission android:name="android.permission.INTERNET" />
在我的清单上的正确位置(应用程序内部),事实上我也有“android.permission.ACCESS_FINE_LOCATION”,但这不会改变任何东西。
Device
这是一款全新的 Nexus-S(已解锁),运行 Android 2.3.4。
失败的代码
这是有问题的方法:(参见行 HttpResponse response = client.execute(post);)
Please note that I've removed the "real values" but rest assured the correct values are in the original code.
public void getAuthentification(View view) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
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", prefs.getString(
"user", "undefined")));
nameValuePairs.add(new BasicNameValuePair("Passwd", prefs
.getString("password", "undefined")));
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();
}
}
我缺少什么?