Skip to main content
4 de 4
añadida la respuesta de ID 138698 como contenido suplementario
lois6b
  • 7.6k
  • 5
  • 33
  • 54

(Android) Leer texto desde fichero en web

estoy haciendo una aplicacion en la cual necesito leer una cadena de un fichero alojado en un hosting y la muestre en un Toast, con el código que adjunto funciona sin problemas desde Eclipse pero en Android Studio no logro hacer que me obtenga dicha cadena. ¿Alguna idea, solución?.

public String lee ()throws Exception{

     URL url = new URL("http://radios-android.000webhostapp.com/fichero_web.txt");
     URLConnection urlConnection = url.openConnection();
     InputStream is = urlConnection.getInputStream();
     BufferedReader br = new BufferedReader(new InputStreamReader(is));
     
     String linea = br.readLine();
     
    br.close();return linea;
}
public void muestra_toast (View v) throws Exception{
        String cadena = new String();

       try{
           cadena=lee();
       }catch (Exception e){}

        Toast.makeText(this, cadena, Toast.LENGTH_LONG).show();
}

Eclipse


Este error lanza al tocar en el cardview que llama a la funcion y la app se cae:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: mazcvei.login_screen, PID: 5409
                  java.lang.IllegalStateException: Could not execute method for android:onClick
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                      at android.view.View.performClick(View.java:5716)
                      at android.widget.TextView.performClick(TextView.java:10926)
                      at android.view.View$PerformClick.run(View.java:22596)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:7325)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                      at android.view.View.performClick(View.java:5716) 
                      at android.widget.TextView.performClick(TextView.java:10926) 
                      at android.view.View$PerformClick.run(View.java:22596) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:7325) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                   Caused by: android.os.NetworkOnMainThreadException
                      at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
                      at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
                      at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
                      at java.net.InetAddress.getAllByName(InetAddress.java:215)
                      at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
                      at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:220)
                      at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:176)
                      at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108)
                      at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:501)
                      at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:484)
                      at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:466)
                      at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:372)
                      at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
                      at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:418)
                      at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:235)
                      at mazcvei.login_screen.MainActivity.lee(MainActivity.java:26)
                      at mazcvei.login_screen.MainActivity.muestra_toast(MainActivity.java:37)
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                      at android.view.View.performClick(View.java:5716) 
                      at android.widget.TextView.performClick(TextView.java:10926) 
                      at android.view.View$PerformClick.run(View.java:22596) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:7325) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)