Search code examples
androidxmlhttprequestandroidhttpclient

Page content to a string - Android


I have the following php page that returns 1 or 0: http://klh-dev.com/lehava/lehava/system/isloggedin.php I'm trying to create a public String that returnes this page content (which is 0 or 1). I tried several times few different things but I got errors.

I tried this:

    public String IsLoggedIn() {
        String url_text = "http://klh-dev.com/lehava/lehava/system/isloggedin.php";
        String response_str = null;
        try {
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet(url_text.toString());
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                response_str = client.execute(request, responseHandler);
        }
        catch (Exception e) {
                System.out.println("There is an error");
        }
        return response_str;
}

I have tried few other things but always same thing happend.

Errors:

11-01 08:27:07.677: I/System.out(24314): android.os.NetworkOnMainThreadException
11-01 08:27:07.677: W/System.err(24314): android.os.NetworkOnMainThreadException
11-01 08:27:07.677: W/System.err(24314):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
11-01 08:27:07.688: W/System.err(24314):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-01 08:27:07.688: W/System.err(24314):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-01 08:27:07.688: W/System.err(24314):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-01 08:27:07.688: W/System.err(24314):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-01 08:27:07.688: W/System.err(24314):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-01 08:27:07.688: W/System.err(24314):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-01 08:27:07.688: W/System.err(24314):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-01 08:27:07.688: W/System.err(24314):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-01 08:27:07.688: W/System.err(24314):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
11-01 08:27:07.688: W/System.err(24314):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
11-01 08:27:07.688: W/System.err(24314):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
11-01 08:27:07.688: W/System.err(24314):    at com.klh.lehava.MainActivity.IsLoggedIn(MainActivity.java:378)
11-01 08:27:07.688: W/System.err(24314):    at com.klh.lehava.MainActivity.selectItem(MainActivity.java:221)
11-01 08:27:07.688: W/System.err(24314):    at com.klh.lehava.MainActivity.access$0(MainActivity.java:170)
11-01 08:27:07.688: W/System.err(24314):    at com.klh.lehava.MainActivity$DrawerItemClickListener.onItemClick(MainActivity.java:166)
11-01 08:27:07.688: W/System.err(24314):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
11-01 08:27:07.688: W/System.err(24314):    at android.widget.AbsListView.performItemClick(AbsListView.java:1114)
11-01 08:27:07.688: W/System.err(24314):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2912)
11-01 08:27:07.688: W/System.err(24314):    at android.widget.AbsListView$3.run(AbsListView.java:3646)
11-01 08:27:07.688: W/System.err(24314):    at android.os.Handler.handleCallback(Handler.java:733)
11-01 08:27:07.688: W/System.err(24314):    at android.os.Handler.dispatchMessage(Handler.java:95)
11-01 08:27:07.688: W/System.err(24314):    at android.os.Looper.loop(Looper.java:136)
11-01 08:27:07.688: W/System.err(24314):    at android.app.ActivityThread.main(ActivityThread.java:5144)
11-01 08:27:07.688: W/System.err(24314):    at java.lang.reflect.Method.invokeNative(Native Method)
11-01 08:27:07.688: W/System.err(24314):    at java.lang.reflect.Method.invoke(Method.java:515)
11-01 08:27:07.688: W/System.err(24314):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-01 08:27:07.688: W/System.err(24314):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
11-01 08:27:07.698: W/System.err(24314):    at dalvik.system.NativeStart.main(Native Method)

Thank you, Morha13


Solution

  • android.os.NetworkOnMainThreadException

    The exception that is thrown when an application attempts to perform a networking operation on its main thread.

    This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged. See the document Designing for Responsiveness. you need add a thread

    new Thread(new Runnable() {

            @Override
            public void run() {
                HttpClient client=new DefaultHttpClient();
                HttpGet get=new HttpGet("http://klh-dev.com/lehava/lehava/system/isloggedin.php");
                 ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String response_str = null;
                try {
                    response_str=client.execute(get,responseHandler);
                    System.out.println(response_str);
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();