本文介绍了从Android设备发送HTTPS / HTTP POST时的UnknownHostException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图描述创建一个HTTP POST到谷歌的服务器获得的ClientLogin验证(的)。该人士$ ​​C $ C的职位是不是一个真正的奥秘,我发现它。 (我敢肯定,我的职务作品,因为使用curl我得到验证)

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).

试图code的各种组合后(在谷歌找到和这里),不同的URL过(有或没有HTTPS),常规的HTTP太多,等我决定尝试以下操作:

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)

现在我已经重新开始肯定Eclipse的电话,我甚至忘记了Wi-Fi并重新创建它。我使用的是谷歌任何的DNS或OpenDNS的手机上的一个静态IP地址(我已经换他们尝试)。因特网在电话作品,其他的应用程序。

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.

我做

<uses-permission android:name="android.permission.INTERNET" />

我在正确的位置(内部应用),其实我也有android.permission.ACCESS_FINE_LOCATION但是,这并不能改变什么。清单

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.

这是一个新鲜的Nexus S(解锁)采用Android 2.3.4。

It's a fresh Nexus-S (unlocked) with Android 2.3.4.

这是有问题的方法:(见线的Htt presponse响应= client.execute(岗位);)

This is the method in question: (see line HttpResponse response = client.execute(post);)

请注意,我已经去掉了实际价值,但放心正确的值是在原来的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();
    }
}

我是什么失踪?

推荐答案

好吧,看完之后,并在相反的是我在其他的谷歌搜索结果读过了...

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" />

...必须应用程序之外

我有:

       <!-- General Permissions -->
       <uses-permission android:name="android.permission.INTERNET" />
    </manifest>

现在它的作品。

这篇关于从Android设备发送HTTPS / HTTP POST时的UnknownHostException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:06
查看更多