I have a problem with connecting to internet. I want to show tbe first line in the source code of www.google.com. I coded in Java console and it run well. But when I did it in android, the textView can't not show any words. Original Java console program:
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
public class ReadUrl {
static public String getRespond(String request) {
try{
URL url=new URL (request);
URLConnection connection=url.openConnection();
InputStream in=new BufferedInputStream(connection.getInputStream());
Scanner scanner =new Scanner(in);
String response=scanner.nextLine();
scanner.close();
return response;
}
catch (Exception e){
return null;
}
}
public static void main(String args[]){
System.out.println (getRespond("http://www.google.com"));
}
}
In android: File MainActivity.java:
package com.example.simpleinternet;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txtView=(TextView) findViewById(R.id.textView);
txtView.setText(getRespond("www.google.com"));
}
public String getRespond(String request) {
try{
URL url=new URL (request);
URLConnection connection=url.openConnection();
InputStream in=new BufferedInputStream(connection.getInputStream());
Scanner scanner =new Scanner(in);
String response=scanner.nextLine();
scanner.close();
return response;
}
catch (IOException e){
return null;
}
}
}
File AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simpleinternet"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.simpleinternet.MainActivity"
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>
File activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World" />
</FrameLayout>
The error is :
> 07-07 14:44:55.760: E/AndroidRuntime(4449):
> Java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.example.simpleinternet/com.example.simpleinternet.MainActivity}:
> android.os.NetworkOnMainThreadException
Sorry because I am a newbie in StackOverFlow. I will take it carefully. Can you check it for me ?