user3148161
user3148161

Reputation: 37

Trouble getting html code from an URL

Please help me out.I am trying to fetch html code from url in Android but there is something wrong with the code which I'm unable to figure it out. Processing is not proceeding beyond this line, "HttpResponse response = httpclient.execute(httpget);"

I also tried doing other way like,

URLConnection connection = (new URL("https://google.com")).openConnection();
Scanner scanner = new Scanner(connection.getInputStream());<--Not executing

but I am facing same issue here as well.

Here is my actual code,

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("https://google.com"); 
HttpResponse response = httpclient.execute(httpget);<--This line is not executing
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) 
            sb.append(line + "\n");

String resString = sb.toString(); 

        is.close();

Here is my log,

11-22 22:58:29.233 5830-5830/com.example.sdp.smeme I/art: Not late-enabling -Xcheck:jni (already on)
11-22 22:58:31.610 5830-5830/com.example.sdp.smeme W/System: ClassLoader referenced unknown path: /data/app/com.example.sdp.smeme-1/lib/arm64
11-22 22:58:31.693 5830-5830/com.example.sdp.smeme I/InstantRun: starting instant run server: is main process
11-22 22:58:32.203 5830-5830/com.example.sdp.smeme W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-22 22:58:33.084 5830-5830/com.example.sdp.smeme D/NetworkSecurityConfig: No Network Security Config specified, using platform default
11-22 22:58:33.284 5830-5830/com.example.sdp.smeme W/gralloc_ranchu: Gralloc pipe failed

                                                                      [ 11-
22 22:58:33.285  5830: 5830 D/         ]
                                                                      HostConnection::get() New Host Connection established 0x78ce5e5900, tid 5830


                                                                      [ 11-
22 22:58:33.545  5830: 5847 D/         ]
                                                                      HostConnection::get() New Host Connection established 0x78ce5e59c0, tid 5847
11-22 22:58:33.552 5830-5847/com.example.sdp.smeme I/OpenGLRenderer: Initialized EGL, version 1.4
11-22 22:58:33.553 5830-5847/com.example.sdp.smeme D/OpenGLRenderer: Swap behavior 1
11-22 22:58:33.553 5830-5847/com.example.sdp.smeme W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
11-22 22:58:33.558 5830-5847/com.example.sdp.smeme D/OpenGLRenderer: Swap behavior 0
11-22 22:58:33.858 5830-5830/com.example.sdp.smeme W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView

Upvotes: 1

Views: 38

Answers (1)

diegoveloper
diegoveloper

Reputation: 103561

If you want to make 'https' connection , you could use this:

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https", 
            SSLSocketFactory.getSocketFactory(), 443));

HttpParams params = new BasicHttpParams();

SingleClientConnManager mgr = new SingleClientConnManager(params, schemeRegistry);

HttpClient client = new DefaultHttpClient(mgr, params);
HttpGet httpget = new HttpGet("https://google.com"); 

Don't forget to declare your internet permission in the manifest file:

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

EDIT

You must run the code on the background thread

Upvotes: 1

Related Questions