-2

Im trying to run this piece of code

            String test;
            Document doc= null;
            try {
                doc = Jsoup.connect("http:\\thatsthefinger.com\\").get();
                test=doc.title();
            } catch (IOException e) {
                e.printStackTrace();
                out.setText(e.getMessage());
            }

this is the execption im getting.

W/System.err: java.net.UnknownHostException: http:\thatsthefinger.com\
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.createAddress(HttpEngine.java:1143)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:323)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:249)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:437)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
W/System.err:     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:652)
W/System.err:     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:629)
W/System.err:     at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:261)
W/System.err:     at org.jsoup.helper.HttpConnection.get(HttpConnection.java:250)
W/System.err:     at gplabs.szalpha.MainActivity$1.onClick(MainActivity.java:62)
W/System.err:     at android.view.View.performClick(View.java:5207)
W/System.err:     at android.view.View$PerformClick.run(View.java:21177)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at     android.app.ActivityThread.main(ActivityThread.java:5441)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

I searched everywhere and did everything i could, but couldn't solve. Anyone at least give me a working piece of code to it on my version of android studio, because i doubt my avm. Thanks in advance.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
PoomaniGP
  • 80
  • 1
  • 1
  • 11

2 Answers2

1

Write it like this

connect("http://www.thatsthefinger.com")

Vedvrat
  • 28
  • 5
  • android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273) at java.net.InetAddress.lookupHostByName(InetAddress.java:431) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) at java.net.InetAddress.getAllByName(InetAddress.java:215) ......it goes like this – PoomaniGP Sep 10 '17 at 13:22
  • @PoomaniGP first of all you said you tried another urls and they also don't work but seems your error was obvious second one you can't download data from internet on UI thread you have to run it in background for example use `AsyncTask` – Yupi Sep 10 '17 at 13:36
  • @Yupi i did use many other urls and everytime i made the same mistake of using 'backslash', secondly i thought of using the AsyncTask but instead i ran a thread to check if it works and it does, anyways thank you a lot. – PoomaniGP Sep 10 '17 at 15:50
0

as Vedvrat said. See at this line your URL is wrong.

java.net.UnknownHostException: http:\thatsthefinger.com\

You have to pass valid URL. Url must be: http://www.thatsthefinger.com

That doesn't give you a new issue NetworkOnMainThreadException.

Actually, you are not allowed to call network operations in MainThread. If it is allowed, The main thread has to wait until the result comes. Result, Your app will stay hanged until the result comes. It will produce bad user experience.

Use AsyncTask to call network operations.

private class DownloadFilesTask extends AsyncTask<String, String, Document> {
     protected Document doInBackground(String... urls) {
        Document doc= null;
        try {
            doc = Jsoup.connect("http:\\thatsthefinger.com\\").get();
        } catch (IOException e) {
            e.printStackTrace();
            out.setText(e.getMessage());
        }
        return doc;
     }

     protected void onPostExecute(Document doc) {
         if(doc != null){
            String test= doc.title();
         }
     }
 }

See here for more info about AsyncTask: https://developer.android.com/reference/android/os/AsyncTask.html

Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66