UnknownHostException when sending HTTPS/HTTP POST from Android Device
I'm trying to create an HTTP POST to Google servers to obtain the ClientLogin Auth (as described here). The source code for the post is not a real mystery and I found it here. (I'm sure my post works because using CURL I obtain the Auth)
The method is very simple and I've modified the values accordingly, however, I'm getting the following exception when I execute the following line:
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…
(irrelevant log omitted).
After trying various combinations of code (found on google and here), different URLs too (with or without HTTPS), regular HTTP too, etc. I've decided to try the following:
try {
InetAddress address = InetAddress.getByName("http://www.google.com");
} catch (UnknownHostException e) {
e.printStackTrace();
}
and I get the same error: (trimmed the irrelevant parts too)
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)
Now I've sure restarted Eclipse, The Phone, I've even forgotten the Wi-Fi and re-created it. I'm using a STATIC IP address on the phone with either Google's DNS or OpenDNS (i've swapped them to try). Internet on the phone works, other applications work.
I do have
<uses-permission android:name="android.permission.INTERNET" />
on my Manifest in the correct spot (inside Application), in fact I also have "android.permission.ACCESS_FINE_LOCATION" but that doesn't change anything.
Device
It's a fresh Nexus-S (unlocked) with Android 2.3.4.
Code that Fails
This is the method 开发者_Python百科in question: (see line 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();
}
}
What am I missing?
Ok, after reading this answer and in contrary to what I've read in other google results, the…
<uses-permission android:name="android.permission.INTERNET" />
…must be outside the application.
I have it:
<!-- General Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Now it works.
Do you try it on Device? Sometimes what happens is that emulator fails to resolve DNS and gives an error message.
精彩评论