从Android设备发送HTTPS/HTTP POST时出现UnknownHostException

Mar*_*ini 4 android exception http http-post

我试图创建一个HTTP POST到谷歌服务器,以获得对ClientLogin验证(如描述在这里).这篇文章的源代码并不是一个真正的谜,我在这里找到了它.(我确定我的帖子有效,因为使用CURL我获得了Auth)

该方法非常简单,我相应地修改了值,但是,当我执行以下行时,我得到以下异常:

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…
Run Code Online (Sandbox Code Playgroud)

(省略了无关的日志).

尝试了各种代码组合(在谷歌和这里找到),不同的URL(有或没有HTTPS),常规HTTP等等.我决定尝试以下方法:

try {
       InetAddress address = InetAddress.getByName("http://www.google.com");
} catch (UnknownHostException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

我得到同样的错误:(也修剪了不相关的部分)

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)
Run Code Online (Sandbox Code Playgroud)

现在我确定重新启动了Eclipse,The Phone,我甚至忘记了Wi-Fi并重新创建了它.我在手机上使用谷歌的DNS或OpenDNS的STATIC IP地址(我已经交换它们尝试).手机上的互联网工作,其他应用程序工作.

<uses-permission android:name="android.permission.INTERNET" /> 
Run Code Online (Sandbox Code Playgroud)

在我的Manifest中的正确位置(在Application中),实际上我也有"android.permission.ACCESS_FINE_LOCATION",但这并没有改变任何东西.

设备

这是一款采用Android 2.3.4的全新Nexus-S(解锁).

代码失败

这是有问题的方法:(参见行HttpResponse response = client.execute(post);)

请注意,我已删除"实际值"但请放心原始代码中的值是否正确.

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Mar*_*ini 6

好的,看完这个答案后,与我在其他谷歌搜索结果中看到的相反,......

<uses-permission android:name="android.permission.INTERNET" /> 
Run Code Online (Sandbox Code Playgroud)

......必须在申请之外.

我有它:

       <!-- General Permissions -->
       <uses-permission android:name="android.permission.INTERNET" />
    </manifest>
Run Code Online (Sandbox Code Playgroud)

现在它有效.