dimanche 29 janvier 2017

Android getting website content using InputStream

I am trying to get the content of a website (its source) in an Android app.

URL url = new URL("http://verbmaps.com/");
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0");
InputStream is = con.getInputStream();

Now this gives a lot of errors when run.

(I have put this is a js snippet so I could hide it)

/*

W/System.err: android.os.NetworkOnMainThreadException
W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:215)
W/System.err:     at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
W/System.err:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
W/System.err:     at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
W/System.err:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
W/System.err:     at com.example.yeeverbs.MainActivity$override.onCreate(MainActivity.java:41)
W/System.err:     at com.example.yeeverbs.MainActivity$override.access$dispatch(MainActivity.java)
W/System.err:     at com.example.yeeverbs.MainActivity.onCreate(MainActivity.java:0)
W/System.err:     at android.app.Activity.performCreate(Activity.java:6237)
W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
W/System.err:     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
W/System.err:     at android.app.ActivityThread.-wrap15(ActivityThread.java)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


*/

One of the errors points to line 41, which is the line with the InputStream:

InputStream is = con.getInputStream();

Hence, I was not able to run it successfully.

Additional Notes

I have the required permissions in my Android Manifest file

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

Also I've seen that several solutions use DefaultHttpClient instead of URL. I tried it out, but Android Studio tells me it is deprecated. So I had to go back to using URL unless there are any other better alternatives that are not deprecated.

Additionally, I'm trying to do this without any external libraries, so I am not going to accept JSoup or any other such libraries.

In short,

I am getting the source of a website using URL. However InputStream is giving me errors.

I want to know why this is happening. Also any better alternatives that are not deprecated or are external libraries to this task will be appreciated.




Aucun commentaire:

Enregistrer un commentaire