I have a txt file at the online storage that my university offers me and I want to read it at an android application. I've managed to do this in Java but when I try to do this in Android nothing happens. The Eclipse suggestions changes the code and nothing works. Please help me I am new to android development.
The a.txt file has content: This is a message!
This is the java code:
import java.io.*; import java.net.*; public class Java_Parser { public static void main(String[] args) throws Exception { URL oracle = new URL("http://cgi.di.uoa.gr/~std10108/a.txt"); BufferedReader in = new BufferedReader( new InputStreamReader(oracle.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } } And the output is, as it should be : This is a message!
I have added the internet permission to the manifest so it's not the problem. I copy paste it in a function on an android project and after the suggestions of Eclipse it turns to this:
//This a function called when a button is clicked public void OnClick(View view){ URL oracle = null; BufferedReader in = null; String inputLine; String text=null; try { oracle = new URL("http://cgi.di.uoa.gr/~std10108/a.txt"); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { in = new BufferedReader( new InputStreamReader(oracle.openStream())); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { while ((inputLine = in.readLine()) != null) text=text + inputLine; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } EditText edittext= (EditText) findViewById(R.id.editText1); edittext.setText(text); } The application compiles succesfully but when I try to run it and click the button, it crashes. Can you help me?
This is the logcat
> 11-20 17:40:04.089: I/Process(9926): Sending signal. PID: 9926 SIG: 9 > 11-20 17:50:37.009: D/libEGL(10534): loaded > /system/lib/egl/libEGL_mali.so 11-20 17:50:37.014: D/libEGL(10534): > loaded /system/lib/egl/libGLESv1_CM_mali.so 11-20 17:50:37.014: > D/libEGL(10534): loaded /system/lib/egl/libGLESv2_mali.so 11-20 > 17:50:37.024: D/(10534): Device driver API match 11-20 17:50:37.024: > D/(10534): Device driver API version: 10 11-20 17:50:37.024: > D/(10534): User space API version: 10 11-20 17:50:37.024: D/(10534): > mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST > 2012 11-20 17:50:37.054: D/OpenGLRenderer(10534): Enabling debug mode > 0 11-20 17:50:39.449: D/AndroidRuntime(10534): Shutting down VM 11-20 > 17:50:39.449: W/dalvikvm(10534): threadid=1: thread exiting with > uncaught exception (group=0x416d22a0) 11-20 17:50:39.454: > E/AndroidRuntime(10534): FATAL EXCEPTION: main 11-20 17:50:39.454: > E/AndroidRuntime(10534): java.lang.IllegalStateException: Could not > execute method of the activity 11-20 17:50:39.454: > E/AndroidRuntime(10534): at > android.view.View$1.onClick(View.java:3704) 11-20 17:50:39.454: > E/AndroidRuntime(10534): at > android.view.View.performClick(View.java:4232) 11-20 17:50:39.454: > E/AndroidRuntime(10534): at > android.view.View$PerformClick.run(View.java:17318) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > android.os.Handler.handleCallback(Handler.java:615) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > android.os.Handler.dispatchMessage(Handler.java:92) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > android.os.Looper.loop(Looper.java:137) 11-20 17:50:39.454: > E/AndroidRuntime(10534): at > android.app.ActivityThread.main(ActivityThread.java:4921) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > java.lang.reflect.Method.invokeNative(Native Method) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > java.lang.reflect.Method.invoke(Method.java:511) 11-20 17:50:39.454: > E/AndroidRuntime(10534): at > com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) > 11-20 17:50:39.454: E/AndroidRuntime(10534): at > com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > dalvik.system.NativeStart.main(Native Method) 11-20 17:50:39.454: > E/AndroidRuntime(10534): Caused by: > java.lang.reflect.InvocationTargetException 11-20 17:50:39.454: > E/AndroidRuntime(10534): at > java.lang.reflect.Method.invokeNative(Native Method) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > java.lang.reflect.Method.invoke(Method.java:511) 11-20 17:50:39.454: > E/AndroidRuntime(10534): at > android.view.View$1.onClick(View.java:3699) 11-20 17:50:39.454: > E/AndroidRuntime(10534): ... 11 more 11-20 17:50:39.454: > E/AndroidRuntime(10534): Caused by: > android.os.NetworkOnMainThreadException 11-20 17:50:39.454: > E/AndroidRuntime(10534): at > android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118) > 11-20 17:50:39.454: E/AndroidRuntime(10534): at > java.net.InetAddress.lookupHostByName(InetAddress.java:385) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > java.net.InetAddress.getAllByName(InetAddress.java:214) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > libcore.net.http.HttpConnection.<init>(HttpConnection.java:70) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) > 11-20 17:50:39.454: E/AndroidRuntime(10534): at > libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) > 11-20 17:50:39.454: E/AndroidRuntime(10534): at > libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315) > 11-20 17:50:39.454: E/AndroidRuntime(10534): at > libcore.net.http.HttpEngine.connect(HttpEngine.java:310) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) > 11-20 17:50:39.454: E/AndroidRuntime(10534): at > libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): at > libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) > 11-20 17:50:39.454: E/AndroidRuntime(10534): at > libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) > 11-20 17:50:39.454: E/AndroidRuntime(10534): at > java.net.URL.openStream(URL.java:462) 11-20 17:50:39.454: > E/AndroidRuntime(10534): at > com.example.test.MainActivity.OnClick(MainActivity.java:55) 11-20 > 17:50:39.454: E/AndroidRuntime(10534): ... 14 more Edit1: I added the Internet permission but nothing changed