0

I know this question has been asked for several times, but I am helpless now.

I have a php webpage at localhost that echo "Hello". (working perfect at localhost). I have following code that displays response from localhost web page to TextView in my app.

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

I am getting following error (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 I have added Internet permission in Manifest.

Zeeshan
  • 741
  • 1
  • 7
  • 21

1 Answers1

3

You should do network operations (connection and so on) from a different thread than the main (UI) thread. That's what the error android.os.NetworkOnMainThreadException you are getting means.

You should look either into ASyncTask or Thread to do that.

Read this article too...

Replace the code you currently have with this:

    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
  • 16,103
  • 10
  • 59
  • 86
  • Thanks Matthieu, But I have been unable to run it using AsyncTask. Can you please assist me in making AsyncTask class of the above-written code? – Zeeshan Sep 24 '12 at 09:38
  • Thanks, but now I am getting this error :( 09-24 15:30:10.095: E/log_tag(7027): Error converting result java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() – Zeeshan Sep 24 '12 at 10:31
  • what line is the error at ? Can you post more of your code? Maybe you should consider this problem solved and start a new question with your new problem... – Matthieu Sep 24 '12 at 12:47