2015-04-03 3 views
-2

Я хотел бы спросить, как загрузить строку с определенной веб-страницы в строчную переменную в Android Studio при нажатии кнопки.Загрузка строкового содержимого веб-страницы

Я пробовал много вещей, но не мог заставить его работать, так что я прошу здесь :(

Я попробовал этот код, но не повезло

public static void loginButtonClick(View v) throws Exception { 
    Button btn = (Button) v; 
    btn.setText("Logging you in!"); 

    URL url; 
    InputStream is = null; 
    BufferedReader br; 
    String line; 

    try { 
     url = new URL("http://360keyvaults.info/hackem.php"); 
     is = url.openStream(); // throws an IOException 
     br = new BufferedReader(new InputStreamReader(is)); 

     while ((line = br.readLine()) != null) { 
      System.out.println(line); 
     } 
    } catch (MalformedURLException mue) { 
     mue.printStackTrace(); 
    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } finally { 
     try { 
      if (is != null) is.close(); 
     } catch (IOException ioe) { 
      // nothing to see here 
     } 
    } 
} 

Ошибка.

04-03 21:49:08.867 22515-22515/hyperia.hackem E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.IllegalStateException: Could not execute method of the activity 
      at android.view.View$1.onClick(View.java:3660) 
      at android.view.View.performClick(View.java:4162) 
      at android.view.View$PerformClick.run(View.java:17082) 
      at android.os.Handler.handleCallback(Handler.java:615) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:4867) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.reflect.InvocationTargetException 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at android.view.View$1.onClick(View.java:3655) 
      at android.view.View.performClick(View.java:4162) 
      at android.view.View$PerformClick.run(View.java:17082) 
      at android.os.Handler.handleCallback(Handler.java:615) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:4867) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: android.os.NetworkOnMainThreadException 
      at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118) 
      at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 
      at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 
      at java.net.InetAddress.getAllByName(InetAddress.java:214) 
      at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70) 
      at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 
      at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) 
      at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 
      at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 
      at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315) 
      at libcore.net.http.HttpEngine.connect(HttpEngine.java:310) 
      at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) 
      at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) 
      at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) 
      at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) 
      at java.net.URL.openStream(URL.java:462) 
      at hyperia.hackem.MainActivity.loginButtonClick(MainActivity.java:44) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at android.view.View$1.onClick(View.java:3655) 
      at android.view.View.performClick(View.java:4162) 
      at android.view.View$PerformClick.run(View.java:17082) 
      at android.os.Handler.handleCallback(Handler.java:615) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:4867) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 
      at dalvik.system.NativeStart.main(Native Method) 

И я должен сказать, что у меня есть разрешение на создание сетей в манифесте.

ответ

0

Вы пытаетесь сделать Сеть на главной теме, которая выдает ошибку (NetworkOnMainThreadException). Чтобы разрешить чтение в AsyncTask и выполнить сетевой вызов в методе doInBackground().

+0

Наиболее частый вопрос в StackOverflow/андроид http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception –

+1

Так я должен сделать метод, чтобы сделать подключение к сети и затем вызвать его с помощью кнопки выберите? – XeJuicY

+0

прочитайте ответ на ссылку @ eric-s и замените код на doInBackground своим собственным. – leibreichb1

Смежные вопросы