Elijah Elijah - 2 months ago 29
Java Question

Stop working Error while getting data from url using Jsoup

I am a amateur in android programming.I was using Jsoup for getting html from a website and putting it into a textview but i got error .please help me.

My code:

package com.example.hashtag.asdasd;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;



public class MainActivity extends AppCompatActivity {


Document siteDoc;
String a;
Elements p;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn= (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new siteGetter().execute();
}
});

}

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


@Override
protected String doInBackground(Void... voids) {
try {

siteDoc = Jsoup.connect("http://hashtagedu.ir").get();



} catch (IOException ex) {
ex.printStackTrace();
}
return null;

}



}
public void displaydata(){
p = siteDoc.getElementsByTag("p");
a=p.text();
TextView tv= (TextView) findViewById(R.id.textView);
tv.setText(a);
}
}


My Errors:


java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at
java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.SecurityException: Permission denied (missing
INTERNET permission?)
at java.net.InetAddress.lookupHostByName(InetAddress.java:464)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at
com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at
com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at
com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at
com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at
com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
at
com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
at
com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at
com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at
com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:437)
at
com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at
org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:563)
at
org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:540)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:216)
at
com.example.hashtag.asdasd.MainActivity$siteGetter.doInBackground(MainActivity.java:46)
at
com.example.hashtag.asdasd.MainActivity$siteGetter.doInBackground(MainActivity.java:39)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 
Caused by: android.system.GaiException: android_getaddrinfo failed:
EAI_NODATA (No address associated with hostname)
at libcore.io.Posix.android_getaddrinfo(Native Method)
at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 
at java.net.InetAddress.getAllByName(InetAddress.java:215) 
at
com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29) 
at
com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188) 
at
com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157) 
at
com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100) 
at
com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357) 
at
com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340) 
at
com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330) 
at
com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248) 
at
com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:437) 
at
com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114) 
at
org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:563) 
at
org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:540) 
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227) 
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:216) 
at
com.example.hashtag.asdasd.MainActivity$siteGetter.doInBackground(MainActivity.java:46) 
at
com.example.hashtag.asdasd.MainActivity$siteGetter.doInBackground(MainActivity.java:39) 
at android.os.AsyncTask$2.call(AsyncTask.java:295) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 
Caused by: android.system.ErrnoException: android_getaddrinfo failed:
EACCES (Permission denied)
at libcore.io.Posix.android_getaddrinfo(Native Method) 
at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55) 
at java.net.InetAddress.lookupHostByName(InetAddress.java:451) 
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 
at java.net.InetAddress.getAllByName(InetAddress.java:215) 
at
com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29) 
at
com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188) 
at
com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157) 
at
com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100) 
at
com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357) 
at
com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340) 
at
com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330) 
at
com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248) 
at
com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:437) 
at
com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114) 
at
org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:563) 
at
org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:540) 
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227) 
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:216) 
at
com.example.hashtag.asdasd.MainActivity$siteGetter.doInBackground(MainActivity.java:46) 
at
com.example.hashtag.asdasd.MainActivity$siteGetter.doInBackground(MainActivity.java:39) 
at android.os.AsyncTask$2.call(AsyncTask.java:295) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818)

Answer

You must add internet permission to your manifest.

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