-1

Aqui esta el error:

2019-01-19 21:25:53.801 25369-25369/RubenPX.ROX E/AndroidRuntime: FATAL EXCEPTION: main
Process: RubenPX.ROX, PID: 25369
android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1450)
    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
    at java.net.InetAddress.getAllByName(InetAddress.java:787)
    at com.android.okhttp.Dns$1.lookup(Dns.java:39)
    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
    at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
    at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
    at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
    at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538)
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(Unknown Source:0)
    at RubenPX.ROX.WebAPI.WebApi.GetWebResponse(WebApi.java:21)
    at RubenPX.ROX.WebAPI.LogIn.LogIn(LogIn.java:9)
    at RubenPX.ROX.Main$LogIn.onClick(Main.java:31)
    at android.view.View.performClick(View.java:6294)
    at android.view.View$PerformClick.run(View.java:24770)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6520)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Aqui esta el codigo:

public static String GetWebResponse(final String url) {
    try {
        // Create URL
        URL githubEndpoint = new URL("https://api.github.com/");

        // Create connection
        HttpsURLConnection myConnection = (HttpsURLConnection) githubEndpoint.openConnection();

        myConnection.setRequestProperty("User-Agent", "my-rest-app-v0.1");

        if (myConnection.getResponseCode() == 200) {
            InputStream responseBody = myConnection.getInputStream();
            InputStreamReader responseBodyReader = new InputStreamReader(responseBody, "UTF-8");


            return responseBodyReader.toString();
        } else {
            return "Error: " + myConnection.getResponseCode();
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

Llamada

String Res = WebApi.GetWebResponse("http://www.google.com");

Lo que estoy intentando hacer, es conseguir el contenido de una URL, pero no me deja.

2
  • hay una parte del error que dice Cauded By pero no la has puesto Commented el 19 ene. 2019 a las 20:38
  • En que linea dices? no lo encuentro
    – Ruben-PX
    Commented el 19 ene. 2019 a las 20:42

1 respuesta 1

0

Acabo de encontrar la respuesta sin querer: aqui la teneis.

How do I fix android.os.NetworkOnMainThreadException?

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.