Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8572613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:03:38+00:00 2026-06-11T19:03:38+00:00

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

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:03:40+00:00Added an answer on June 11, 2026 at 7:03 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked several times for sure, but I have
I know this question has been asked several times, but I can't quite seem
I know that this question have been asked several times. But I can't get
I know this question has been asked several times on SO, but none of
I know this question has been asked several times but the answers given don't
I know this Question has been asked several times before, and I have tried
I know this question has been asked several times, but I would like to
I know that this question has been asked several times and I've read all
I know this question has been asked a zillion times, but I still feel
I know this question has been asked before numorous times but they seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.