ANDROID - Reading file from localhost (ERROR)
<p>I have a code which load a textfile from my localhost, i am using xampp, i put my file on xampp folder under htdocs. here is my code:</p>

<pre><code>public class MainActivity extends Activity {

TextView httpStuff;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    httpStuff = (TextView)findViewById(R.id.tvHttp);        
    try {
        URL url = new URL("http://10.0.2.2/name.txt");
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));           
        String str = in.readLine();
        in.close();
        httpStuff.setText(str);
        } catch (MalformedURLException e) {  
        e.printStackTrace();
    }catch (IOException e) {
        // TODO: handle exception
    }

}}
</code></pre>

<p>and I got this error. I don't know what's wrong. I have also added permission internet in my androidmanifest.xml file.</p>

<pre><code>   10-24 17:17:45.024: E/AndroidRuntime(7544): FATAL EXCEPTION: main
10-24 17:17:45.024: E/AndroidRuntime(7544): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.name.in.a.textfile/com.example.name.in.a.textfile.MainActivity}: android.os.NetworkOnMainThreadException
10-24 17:17:45.024: E/AndroidRuntime(7544):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at android.os.Looper.loop(Looper.java:137)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at java.lang.reflect.Method.invokeNative(Native Method)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at java.lang.reflect.Method.invoke(Method.java:511)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at dalvik.system.NativeStart.main(Native Method)
10-24 17:17:45.024: E/AndroidRuntime(7544): Caused by: android.os.NetworkOnMainThreadException
10-24 17:17:45.024: E/AndroidRuntime(7544):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at libcore.io.IoBridge.connect(IoBridge.java:112)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at java.net.Socket.connect(Socket.java:842)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:76)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:50)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at java.net.URL.openStream(URL.java:462)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at com.example.name.in.a.textfile.MainActivity.onCreate(MainActivity.java:27)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at android.app.Activity.performCreate(Activity.java:5008)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-24 17:17:45.024: E/AndroidRuntime(7544):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-24 17:17:45.024: E/AndroidRuntime(7544):     ... 11 more
</code></pre>
