1

Trying to push html code for the page in a variable and display it, but the performance of this code:

private class SearchTask extends AsyncTask<String, Void, String> {

String http = "";

@Override
protected String doInBackground(String... params) {
    StringBuilder builder = new StringBuilder(16384);

    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet("https://www.google.com.ua/");

    try {
        HttpResponse responce = client.execute(get);
        InputStream content = responce.getEntity().getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(content));
        while ((http = reader.readLine()) != null) {
            builder.append(http);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return builder.toString();
}

@Override
protected void onPostExecute(String s) {
    editTxt.setText(s);

}

} flies error and application crash.

I can not understand what the problem is. Authorization for access to the Internet is given in the manifesto. Tell me what's wrong? Maybe there is a way easier to do this?

logcat:

08-08 16:54:25.030  23105-23236/com.example.ron.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #3
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
        at java.util.concurrent.FutureTask.run(FutureTask.java:239)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:838)
 Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:430)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
        at java.net.InetAddress.getAllByName(InetAddress.java:214)
        at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
        at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
        at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
        at com.example.ron.myapplication.MainActivity$SearchTask.doInBackground(MainActivity.java:93)
        at com.example.ron.myapplication.MainActivity$SearchTask.doInBackground(MainActivity.java:80)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname) 
        at libcore.io.Posix.getaddrinfo(Native Method)
        at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:405) 
Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this Also note that HttpClient was deprecated in Android 5.1 and is removed from the SDK in the M Developer Preview. Please consider using other HTTP options, such as OkHttp. – CommonsWare Aug 08 '15 at 13:38

2 Answers2

0

In AndroidManifest.xml file, you should add

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

Its location in AndroidManifest.xml looks like the following:

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

 <application ...

</manifest>

UPDATE: If permission already set, did you run in AVD or real phone? If AVD, check your Network connection in your PC if it can access Internet or not, perhaps try to open that Url in web browser first (do the same in real phone).

BNK
  • 23,994
  • 8
  • 77
  • 87
  • Authorization for access to the Internet is given in the manifest – Роман Юхно Aug 08 '15 at 16:47
  • Did you run in AVD or real phone? If AVD, check your Network connection in your PC if it can access Internet or not, perhaps try to open that Url in web browser first (do the same in real phone). – BNK Aug 08 '15 at 22:43
0

the Internet permission was not declared in AndroidManifest yet. Review your doInBackGround, you're missing somethings...