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 8915109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:53:55+00:00 2026-06-15T04:53:55+00:00

I figured out that my tablet with android 4.0.3 stops here while my phone

  • 0

I figured out that my tablet with android 4.0.3 stops here while my phone with 2.2.3 works on this code but I do not know why:

I can not think of a difference between the tablet and phone coding, someone?

/* Store this device macaddress within the server. */
    static void storeMacAddress(final Context context, final String macAddr) { 
        Log.i(TAG, "Store device on Server (macAddress = " + macAddr + ")");
        String serverUrl = SERVER_URLL;
        Map<String, String> params = new HashMap<String, String>();
        params.put("macaddress", macAddr);

        long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
        // As the server might be down, we will retry it a couple times.
        for (int i = 1; i <= MAX_ATTEMPTS; i++) {
            Log.d(TAG, "Attempt #" + i + " to store");
            try {
                post(serverUrl, params);
                Log.d(TAG, "Store op Server gelukt!");
                return;
            } catch (IOException e) {
                // Here we are simplifying and retrying on any error
                Log.e(TAG, "Failed to store on attempt " + i + ":" + e);
                if (i == MAX_ATTEMPTS) {
                    break;
                }
                try {
                    Log.d(TAG, "Sleeping for " + backoff + " ms before retry");
                    Thread.sleep(backoff);
                } catch (InterruptedException e1) {
                    // Activity finished before we complete - exit.
                    Log.d(TAG, "Thread interrupted: abort remaining retries!");
                    Thread.currentThread().interrupt();
                    return;
                }
                // increase backoff exponentially
                backoff *= 2;
            } 
        }

        Log.d(TAG, "Error tijdens store op Server procedure!");        
    }

private static void post(String endpoint, Map<String, String> params)
            throws IOException {    

        URL url;
        try {
            url = new URL(endpoint);
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("invalid url: " + endpoint);
        }
        StringBuilder bodyBuilder = new StringBuilder();
        Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
        // constructs the POST body using the parameters
        while (iterator.hasNext()) {
            Entry<String, String> param = iterator.next();
            bodyBuilder.append(param.getKey()).append('=')
                    .append(param.getValue());
            if (iterator.hasNext()) {
                bodyBuilder.append('&');
            }
        }
        String body = bodyBuilder.toString();
        Log.v(TAG, "Posting '" + body + "' to " + url);
        byte[] bytes = body.getBytes();
        HttpURLConnection conn = null;
        try {
            Log.e("URL", "> " + url);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setFixedLengthStreamingMode(bytes.length);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded;charset=UTF-8");
            // post the request
            OutputStream out = conn.getOutputStream();
            out.write(bytes);
            out.close();
            // handle the response
            int status = conn.getResponseCode();
            if (status != 200) {
              throw new IOException("Post failed with error code " + status);
            }
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
      }

EDIT:
Calling from:

...    
setContentView(R.layout.activity_main);

            final SharedPreferences prefs = this.getSharedPreferences("nl.easy.winkel", Context.MODE_PRIVATE);

            if(!prefs.getString("macaddress","").equals("send")) { // Send MacAddress once
                prefs.edit().putString("macaddress","send").commit();
                obtainMacAddress();
            }

and 

    private void obtainMacAddress() {
            WifiManager wifiMan = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInf = wifiMan.getConnectionInfo();
            String macAddr = wifiInf.getMacAddress();
            final Context context = this;
            ServerUtilities.storeMacAddress(context, macAddr + "|" + getLocalIpAddress());
    }

        public String getLocalIpAddress() {
            try {
                for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                    NetworkInterface intf = en.nextElement();
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress()) {
                            return inetAddress.getHostAddress().toString();
                        }
                    }
                }
            } catch (SocketException ex) {
                Log.e(TAG, ex.toString());
            }
            return null;
        }

EDIT2: using AsyncTask

AsyncTask<Void, Void, Void> mStoreTask;

private void obtainMacAddress() {
        WifiManager wifiMan = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInf = wifiMan.getConnectionInfo();
        final String macAddr = wifiInf.getMacAddress();
        final Context context = this;

        mStoreTask = new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                // Store on our server
                // On server creates a new user
                ServerUtilities.storeMacAddress(context, macAddr + "|" + getLocalIpAddress());
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                mStoreTask = null;
            }

        };
        mStoreTask.execute(null, null, null);
    }
  • 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-15T04:53:56+00:00Added an answer on June 15, 2026 at 4:53 am

    There are two Solution of this Problem but first one is great solution.

    1) Don’t write network call in Main UI Thread, Use Async Task for that.

    2) Write below code into your MainActivity file after setContentView(R.layout.activity_main); but this is not proper way.

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    

    And below import statement into your java file.

    import android.os.StrictMode;
    

    And see below link for more information.

    Caused by: android.os.NetworkOnMainThreadException

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I figured out that CUDA does not work in 64bit mode on my mac
Ok so I have a bit o' script that works in FF but not
I figured out that a constant literal get's placed in the data segment of
I figured out that of course . and SPACE aren't allowed. Are there other
I've figured out that the problem is only with the Build Settings tab. So
I've figured out that default ReceiveBufferSize (8192) doesn't work for me - I lose
I created an UIViewController subclass, and figured out that the default implementation of -loadView
After some trial and error (ok, just error) I figured out that c089b69c3d contained
I'm writing HFT trading software. I'm trying to optimize it. I figured out that
I've figured out, that it's quite easy to purge a ressource out of Varnish

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.