client.execute中的UnknownHostException(httppost)

时间:2014-02-10 17:08:43

标签: android unknown-host

我每次都得到UnknownHostException,这是我的代码部分:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

我在3号线上有例外,

我已重启我的Wifi连接。 我也使用过我的数据计划。 我也尝试使用HttpGet类而不是HttpPost 网址在浏览器上完美运行

我无法理解,实际原因是什么,请帮助我......

这是logcat o / p: -

02-10 22:41:33.307: W/System.err(16016): java.net.UnknownHostException: Unable to resolve host "www.offerpunch.com": No address associated with hostname
02-10 22:41:33.317: W/System.err(16016):    at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
02-10 22:41:33.317: W/System.err(16016):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-10 22:41:33.317: W/System.err(16016):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-10 22:41:33.317: W/System.err(16016):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:141)
02-10 22:41:33.317: W/System.err(16016):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-10 22:41:33.317: W/System.err(16016):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-10 22:41:33.317: W/System.err(16016):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
02-10 22:41:33.317: W/System.err(16016):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-10 22:41:33.317: W/System.err(16016):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-10 22:41:33.317: W/System.err(16016):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-10 22:41:33.317: W/System.err(16016):    at com.android.offerpunch.util.JSONParserForArray.getJSONFromUrl(JSONParserForArray.java:38)
02-10 22:41:33.317: W/System.err(16016):    at com.android.offerpunch.SearchResultActivity$OffersList.doInBackground(SearchResultActivity.java:103)
02-10 22:41:33.317: W/System.err(16016):    at com.android.offerpunch.SearchResultActivity$OffersList.doInBackground(SearchResultActivity.java:1)
02-10 22:41:33.317: W/System.err(16016):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-10 22:41:33.317: W/System.err(16016):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-10 22:41:33.317: W/System.err(16016):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-10 22:41:33.317: W/System.err(16016):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-10 22:41:33.317: W/System.err(16016):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-10 22:41:33.317: W/System.err(16016):    at java.lang.Thread.run(Thread.java:856)
02-10 22:41:33.327: W/System.err(16016): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
02-10 22:41:33.327: W/System.err(16016):    at libcore.io.Posix.getaddrinfo(Native Method)
02-10 22:41:33.327: W/System.err(16016):    at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
02-10 22:41:33.327: W/System.err(16016):    at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
02-10 22:41:33.327: W/System.err(16016):    ... 18 more
02-10 22:41:33.327: E/Buffer Error(16016): Error converting result java.lang.NullPointerException: lock == null
02-10 22:41:33.327: E/JSON Parser(16016): Error parsing data org.json.JSONException: End of input at character 0 of 
02-10 22:41:33.337: W/dalvikvm(16016): threadid=12: thread exiting with uncaught exception (group=0x41458ae0)
02-10 22:41:33.337: E/AndroidRuntime(16016): FATAL EXCEPTION: AsyncTask #2
02-10 22:41:33.337: E/AndroidRuntime(16016): java.lang.RuntimeException: An error occured while executing doInBackground()
02-10 22:41:33.337: E/AndroidRuntime(16016):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-10 22:41:33.337: E/AndroidRuntime(16016):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
02-10 22:41:33.337: E/AndroidRuntime(16016):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
02-10 22:41:33.337: E/AndroidRuntime(16016):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
02-10 22:41:33.337: E/AndroidRuntime(16016):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-10 22:41:33.337: E/AndroidRuntime(16016):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-10 22:41:33.337: E/AndroidRuntime(16016):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-10 22:41:33.337: E/AndroidRuntime(16016):    at java.lang.Thread.run(Thread.java:856)
02-10 22:41:33.337: E/AndroidRuntime(16016): Caused by: java.lang.NullPointerException
02-10 22:41:33.337: E/AndroidRuntime(16016):    at com.android.offerpunch.SearchResultActivity$OffersList.doInBackground(SearchResultActivity.java:104)
02-10 22:41:33.337: E/AndroidRuntime(16016):    at com.android.offerpunch.SearchResultActivity$OffersList.doInBackground(SearchResultActivity.java:1)
02-10 22:41:33.337: E/AndroidRuntime(16016):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-10 22:41:33.337: E/AndroidRuntime(16016):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-10 22:41:33.337: E/AndroidRuntime(16016):    ... 4 more

1 个答案:

答案 0 :(得分:1)

确保传递给HttpPost的网址字符串以http://

开头

编辑:我能够使用以下代码示例执行请求(请注意http前缀)

private void httpStuff() {
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            BufferedReader reader = null;
            try {
                String url = "http://www.offerpunch.com/parser/index.php/deals?SR=1&cat_id=&SRcat_id=&SRkeyword=hddhh&deal_city=&deal_area=";
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                InputStream is = httpEntity.getContent();
                reader = new BufferedReader(new InputStreamReader(is));

                StringBuilder total = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    total.append(line);
                }
                return total.toString();

            } catch (ClientProtocolException ex) {
                Log.e("LOG_TAG", null, ex);
            } catch (IOException ex) {
                Log.e("LOG_TAG", null, ex);
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException ex) {
                    }
                }
            }
            return null;
        }

        protected void onPostExecute(String result) {
            if (result == null) {
                Log.d("LOG_TAG", "Result is null");
            } else {
                Log.d("LOG_TAG", "Result size: " + result.length());
                Log.d("LOG_TAG", result);
            }

        };
    }.execute();
}

结果是一个巨大的字符串(长度约为900k),我甚至不确定用户是否能够真正理解解析后的数据......但这不是主题。

在您的情况下,我相信您要么将Internet权限放在错误的地方noted here,要么就是在蹩脚的模拟器上运行应用,或者您根本就没有连接在您运行该应用的设备上。

还有一件事:你得到的NullPointerException是因为你认为服务器端的结果是正确的。在解析之前确保你有非空检查...