-2

i would like to ask how could i download a string from a specific webpage into string variable in Android Studio when i click button.

I tried a lot of things but couldn't make it work so i am asking here :(

I tried this code but no luck.

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
        }
    }
}

Error:

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)

And i have to say that i have the permission for networking in Manifest.

Selvin
  • 6,598
  • 3
  • 37
  • 43
XeJuicY
  • 51
  • 8

1 Answers1

0

You are trying to do Network on the Main Thread which throws an error (NetworkOnMainThreadException). To solve read up on AsyncTask and make the network call in the doInBackground() method.

leibreichb1
  • 457
  • 2
  • 7
  • Most frequent question in stackoverflow/android http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Eric S. Apr 03 '15 at 20:13
  • 1
    So i should make method to do the networking and then call it from the button click? – XeJuicY Apr 03 '15 at 20:18
  • read the answer on the link @eric-s provided and replace the code in doInBackground with your own. – leibreichb1 Apr 03 '15 at 20:23
  • I don't know what should i make doInbackground.... answer in link is not easy enough to solve my problem with it by my own. – XeJuicY Apr 03 '15 at 20:29
  • protected RSSFeed doInBackground(String... urls) { try { is = urls[0].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 } } return null; } – leibreichb1 Apr 03 '15 at 20:33
  • I know but this is not same for me, i just want to download the content of it and compare it ... I could have it as boolean but i dont know if i could do it like that "protected boolean doInBackground(String url) – XeJuicY Apr 03 '15 at 20:38
  • You might as well learn it; you won't be able to make Android apps without learning stuff like this all the time. AsyncTask in particular is very important. After that, you can use libraries like Ion to save you time. – Eric S. Apr 03 '15 at 21:17
  • Well how can i learn if i don't know how -_- I am learning by this. And i tried something but it doesn't work `public class doLogin extends AsyncTask{ private Exception exception; protected Boolean doInBackground(String... params) { try { String user = params[0]; String pass = params[1]; return false; } catch (Exception e) { this.exception = e; return true; } } }` – XeJuicY Apr 03 '15 at 21:18
  • `String test = new doLogin().execute("Test", "Teste").toString(); btn.setText(test);` – XeJuicY Apr 03 '15 at 21:18