0

この質問が何度か聞かれたことは知っていますが、今はどうすることもできません。

「こんにちは」をエコーする localhost に php Web ページがあります。(localhostで完璧に動作します)。アプリの localhost Web ページから 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

PSマニフェストにインターネット許可を追加しました。

4

1 に答える 1

3

メイン (UI) スレッドとは別のスレッドからネットワーク操作 (接続など) を行う必要があります。それが、あなたが得ているエラーのandroid.os.NetworkOnMainThreadException意味です。

それを行うには、ASyncTask または Thread のいずれかを調べる必要があります。

こちらの記事も読んで…

現在持っているコードを次のコードに置き換えます。

    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();
于 2012-09-24T08:49:24.680 に答える