質問

I have get UnknownHostException every time, This is my part of code:

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

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

I have got exception on line no 3,

I have restart my Wifi connection. I have also used my data plan. I have also try with HttpGet class instead of HttpPost The url is work perfectly on the browser

I cant understand, What is the actual cause, Pls help me...

This is 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
役に立ちましたか?

解決

Make sure the url string that you pass to HttpPost begins with http://

EDIT: I was able to perform a request with below code sample (notice the http prefix)

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();
}

The result is a huge string (~900k length), I am not even sure the user can actually understand the parsed data ... but that's off-topic.

In your case, I believe you either put the Internet permission in a wrong place as noted here, or you're running the app on a crappy emulator, or you simply don't have connectivity on the device that you're running the app.

One more thing: that NullPointerException that you get is because you assume the result is correct from server side. Make sure you have not-null checks before parsing ...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top