页面内容为字符串 - Android

时间:2014-11-01 06:20:37

标签: android xmlhttprequest androidhttpclient

我有以下返回1或0的php页面:http://klh-dev.com/lehava/lehava/system/isloggedin.php 我试图创建一个返回此页面内容(0或1)的公共字符串。 我尝试了几次不同的事情,但我遇到了错误。

我试过了:

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

我尝试过其他一些事情,但总会发生同样的事情。

错误:

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)

谢谢你, Morha13

2 个答案:

答案 0 :(得分:0)

查看您的logcat,似乎问题在于您尝试在主UI线程上进行连接。您最好使用AsyncTaskThread并分别在onBackgroundrun中执行操作。

另外,不要忘记添加互联网permission

有一种我不推荐的解决方法:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

onCreate中使用这两个代码可以解决您的错误。但是,它仍然不鼓励在UI线程上进行连接,因为它会使UI滞后甚至无法响应。您必须单独的(后台)主题上建立连接才能获得最佳效果。

请查看此处以获取有关AsyncTask的更多信息(仅示例):http://developer.android.com/reference/android/os/AsyncTask.html

答案 1 :(得分:0)

android.os.NetworkOnMainThreadException

当应用程序尝试在其主线程上执行网络操作时引发的异常。

仅针对Honeycomb SDK或更高版本的应用程序进行此操作。针对早期SDK版本的应用程序可以在其主要事件循环线程上进行网络连接,但是非常不鼓励这样做。请参阅文档设计响应性。 你需要添加一个线程

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();