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

  • Home
  • SEARCH
  • 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 8683681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:06:45+00:00 2026-06-12T22:06:45+00:00

So when i try to run the following code i can never ever get

  • 0

So when i try to run the following code i can never ever get past the response line. what am i doing wrong? I have allowed internet access in my manifest file. I must be missing something huge because ive seen about 100 similar examples that have the exact same code as i do. I’ve even built sample projects just to run these few lines and they all act the same way. please please help (the end goal is to make this work with https as well but any progress to make it work with http is good enough for me)

try{

                        HttpClient myHttpClient = new DefaultHttpClient();
                        HttpPost myHttpPost = new HttpPost("http://www.siirretytnumerot.fi/index.html?clientLanguage=eng");
                       //^Ive tried about 10 different sites (http and https) none of them work

                        //NEVER GETS PAST THE LINE BELOW, JUMPS TO THE LAST CATCH BLOCK OF THE EXCEPTION
                        HttpResponse response = myHttpClient.execute(myHttpPost);
                        //NEVER GETS PAST THE ABOVE LINE...

                        String data = EntityUtils.toString(response.getEntity());
                        json= new JSONObject(data);
                    //parse the JSONObject
                       } catch (UnsupportedEncodingException e){e.printStackTrace();}
                         catch (ClientProtocolException e){e.printStackTrace();}
                         catch (IOException e){e.printStackTrace();}
                         catch (JSONException e) {e.printStackTrace();}
                         catch (NullPointerException e){ Log.e("My APP", "exception: " + e.getMessage());}
                         catch (Exception e ) {Log.e("My APP", "exception", e);} 


10-17 15:31:41.423: E/My APP(974): exception
10-17 15:31:41.423: E/My APP(974): android.os.NetworkOnMainThreadException
10-17 15:31:41.423: E/My APP(974):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
10-17 15:31:41.423: E/My APP(974):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
10-17 15:31:41.423: E/My APP(974):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
10-17 15:31:41.423: E/My APP(974):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
10-17 15:31:41.423: E/My APP(974):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-17 15:31:41.423: E/My APP(974):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-17 15:31:41.423: E/My APP(974):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-17 15:31:41.423: E/My APP(974):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-17 15:31:41.423: E/My APP(974):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-17 15:31:41.423: E/My APP(974):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-17 15:31:41.423: E/My APP(974):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-17 15:31:41.423: E/My APP(974):  at com.example.httpstuff.MainActivity$1.onClick(MainActivity.java:44)
10-17 15:31:41.423: E/My APP(974):  at android.view.View.performClick(View.java:3511)
10-17 15:31:41.423: E/My APP(974):  at android.view.View$PerformClick.run(View.java:14105)
10-17 15:31:41.423: E/My APP(974):  at android.os.Handler.handleCallback(Handler.java:605)
10-17 15:31:41.423: E/My APP(974):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-17 15:31:41.423: E/My APP(974):  at android.os.Looper.loop(Looper.java:137)
10-17 15:31:41.423: E/My APP(974):  at android.app.ActivityThread.main(ActivityThread.java:4424)
10-17 15:31:41.423: E/My APP(974):  at java.lang.reflect.Method.invokeNative(Native Method)
10-17 15:31:41.423: E/My APP(974):  at java.lang.reflect.Method.invoke(Method.java:511)
10-17 15:31:41.423: E/My APP(974):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-17 15:31:41.423: E/My APP(974):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-17 15:31:41.423: E/My APP(974):  at dalvik.system.NativeStart.main(Native Method)
  • 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-12T22:06:46+00:00Added an answer on June 12, 2026 at 10:06 pm

    Perfect, that’s what I was looking for. Looking up NetworkOnMainThreadException, we get this page. What it means is that you can’t run an HTTP request on the main thread of your app, but only when you target Honeycomb.

    So, the easiest way to fix this is to target a different Android version. Maybe 2.3, or up to Jellybean. Another way to solve it is to set up an AsyncTask to run the HTTP request.

    If it works for you to target a different Android version, do that. If not, I can help you out with the AsyncTask. However, keep in mind that they highly reccommend that you don’t run networking requests on your main thread.

    As for AsyncTasks, the best place to turn is always the Android documentation.

    The basic idea is that you extend the AsyncTask class, which makes you implement a doInBackground method. Then you create an instance of your new class, and call execute(). Here’s a code snippet from the documentation:

    private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
        protected Long doInBackground(URL... urls) {
            int count = urls.length;
            long totalSize = 0;
            for (int i = 0; i < count; i++) {
                totalSize += Downloader.downloadFile(urls[i]);
                publishProgress((int) ((i / (float) count) * 100));
                // Escape early if cancel() is called
                if (isCancelled()) break;
            }
            return totalSize;
        }
    
        protected void onProgressUpdate(Integer... progress) {
            setProgressPercent(progress[0]);
        }
    
        protected void onPostExecute(Long result) {
            showDialog("Downloaded " + result + " bytes");
        }
    }
    
    new DownloadFilesTask().execute(url1, url2, url3);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code, and when I try to run it, I can
When I try to run a routine I get the following error: Error Code:
I have the following code: public void run() { try { logger.info(Looking for new
I can't seem to run the following code in Eclipse. I do have a
I have a thread that executes the following code: public void run() { try
I have the following code in SQL developer, and when i try and run
Whenever I try to run anything in my C# code I get the following
I try to run the following code: public void Init(Url rootUrl) { var web
When I try to run the following code from the REPL (playing with dynamic
When I try to run the following, I get an error back from ActiveRecord

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.