Reputation: 1
This is my code, in emulator the code work correctly and print
<!doctype html>
<html itemscope="" itemtype="http://schema.org/WebPage">
<head>
<meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for." name="description">
<meta content="noodp" name="robots"><meta content="/images/google_favicon_128.png" itemprop="image"><meta content="origin" id="mref" name="referrer"><title>Google</title>
<script>(function(){
but in my phone the program unfortunately closed !!
so the program run correctly in my emulator and forced closed in my phone. in my manifest i set internet permision
package com.example.untitled11;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
public class MyActivity extends Activity {
static URL url = null;
static TextView textView;
static ArrayList<String> html = new ArrayList<String> (1000) ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView);
try {
url = new URL("http://google.com/");
} catch (MalformedURLException e) {
textView.setText("URL\n"+e);
}
InputStream xx = null;
try {
xx = url.openConnection().getInputStream();
} catch (IOException e) {
textView.setText("InputStream\n" + e);
}
BufferedReader reader = null;
reader = new BufferedReader( new InputStreamReader( xx ));
String line = "";
try {
while( (line = reader.readLine() ) != null ) {
html.add(line+"");
}
} catch (IOException e) {
textView.setText("adding\n" + e);
}
try {
reader.close();
} catch (IOException e) {
textView.setText("closing\n"+e);
}
textView.setText(html.get(0)+"");
}
}
This is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.untitled11"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:label="@string/app_name">
<activity android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 0
Views: 1494
Reputation: 3322
Use Jsoup Library for HTml Parsing for network operation use asynctask or below code
public static int SDK_INT = android.os.Build.VERSION.SDK_INT;
if (SDK_INT >= 10) {
ThreadPolicy tp = ThreadPolicy.LAX;
StrictMode.setThreadPolicy(tp);
}
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(
"yourURL");
setDefaulHeadersForGet(request);
HttpResponse response = client.execute(request);
String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line);
}
in.close();
html = str.toString();
html = "" + Html.fromHtml(html);
Document doc = Jsoup.parse(html);
Upvotes: 0
Reputation: 23638
Use a thread and a handler to easily run the network related operations
//Handler to send commands to UI thread
Handler handler = new Handler();
Thread th = new Thread(new Runnable() {
public void run() {
try {
url = new URL("http://google.com/");
} catch (MalformedURLException e) {
textView.setText("URL\n"+e);
}
InputStream xx = null;
try {
xx = url.openConnection().getInputStream();
} catch (IOException e) {
textView.setText("InputStream\n" + e);
}
BufferedReader reader = null;
reader = new BufferedReader( new InputStreamReader( xx ));
String line = "";
try {
while( (line = reader.readLine() ) != null ) {
html.add(line+"");
}
} catch (IOException e) {
textView.setText("adding\n" + e);
}
try {
reader.close();
} catch (IOException e) {
textView.setText("closing\n"+e);
}
}
});
th.start();
Upvotes: 1