【问题标题】:Error in connecting localhost连接本地主机时出错
【发布时间】:2012-09-24 08:45:34
【问题描述】:

我知道这个问题已经被问过好几次了,但我现在很无奈。

我在 localhost 有一个 php 网页,它回显“Hello”。 (在本地主机上完美运行)。 我有以下代码显示从本地主机网页到我的应用程序中的 TextView 的响应。

tv = (TextView) findViewById(R.id.txtTest);
InputStream is = null;
String result = "";
    String url = "http://10.0.2.2/android/try.php";
    HttpClient httpclient = new DefaultHttpClient();

    try {               
        HttpPost httppost = new HttpPost(url);

        HttpResponse response = httpclient.execute(httppost);

        Log.d("myapp", "response " + response.getEntity());

        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        String st = EntityUtils.toString(response.getEntity());


    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

    // convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    tv.setText(result.toString());

我收到以下错误 (LogCat)。

09-24 13:34:42.654: E/log_tag(2032): Error in http connection android.os.NetworkOnMainThreadException
09-24 13:34:42.654: E/log_tag(2032): Error converting result java.lang.NullPointerException

P.S 我在 Manifest 中添加了 Internet 权限。

【问题讨论】:

    标签: android httpconnection


    【解决方案1】:

    您应该从不同于主 (UI) 线程的线程执行网络操作(连接等)。这就是你得到的错误android.os.NetworkOnMainThreadException 的含义。

    您应该查看 ASyncTask 或 Thread 来执行此操作。

    也阅读this article...

    用这个替换你当前拥有的代码:

        AsyncTask<Void,Void,Void> my_task = new AsyncTask<Void,Void,Void>() {
            @Override
            protected void onPostExecute() {
                TextView  tv = (TextView) findViewById(R.id.txtTest);
                tv.setText(result.toString());
            }
    
            @Override
            protected Void doInBackground(Void... voids) {
                InputStream is = null;
                String result = "";
                String url = "http://10.0.2.2/android/try.php";
                HttpClient httpclient = new DefaultHttpClient();
    
                try {
                    HttpPost httppost = new HttpPost(url);
    
                    HttpResponse response = httpclient.execute(httppost);
    
                    Log.d("myapp", "response " + response.getEntity());
    
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
                    String st = EntityUtils.toString(response.getEntity());
    
    
                } catch (Exception e) {
                    Log.e("log_tag", "Error in http connection " + e.toString());
                }
    
                // convert response to string
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(
                            is, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();
                    result = sb.toString();
                } catch (Exception e) {
                    Log.e("log_tag", "Error converting result " + e.toString());
                }
            }
        }.execute();
    

    【讨论】:

    • 感谢 Matthieu,但我无法使用 AsyncTask 运行它。您能帮我制作上述代码的 AsyncTask 类吗?
    • 谢谢,但现在我收到此错误:( 09-24 15:30:10.095: E/log_tag(7027): Error conversion result java.lang.RuntimeException: Can't create handler inside没有调用 Looper.prepare() 的线程
    • 错误在哪一行?您可以发布更多代码吗?也许你应该认为这个问题已经解决,并从你的新问题开始一个新问题......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    相关资源
    最近更新 更多