Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't update presence: android.os.NetworkOnMainThreadException #138

Open
GoogleCodeExporter opened this issue Jun 30, 2015 · 4 comments
Open

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?
1.Open RCS-e RI app
2.Click "Presence"
3.Click "Edit my presence info"
4.Enter something
5.Click "Publish"

What is the expected output? What do you see instead?
Expecting: Successfully posting presence information. Message "Publish with 
success"

Actual result: Message box "Publish has failed"

What version of the product are you using? On what operating system?
Android RCS IMS Stack v.2.5.7

Please provide any additional information below.
I've traced this error and found that it is caused by the 
NetworkOnMainThreadException.

Actual issue was actually way below down the stack in the file XdmManager.java 
in the function sendHttpRequest(...) we are opening a socket, which happens to 
be on our main UI thread.

I've fixed it myself by employing the code (I am actually creating separate 
thread to handle Presence Publishing):
file PublishPresenceInfo.java:

/**
 * Publish button listener
 */
private OnClickListener btnPublishPresenceListener = new OnClickListener() {
    public void onClick(View v) {

        //create thread because presense update requires doing network operations,
        //which we cannot do on UI thread
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // Your code goes here

                    // Get the new presence info to be published
                    Spinner statusList = (Spinner) findViewById(R.id.availability);
                    if (statusList.getSelectedItemId() == 0) {
                        presenceInfo.setPresenceStatus(PresenceInfo.ONLINE);
                    } else {
                        presenceInfo
                                .setPresenceStatus(PresenceInfo.OFFLINE);
                    }
                    EditText freetextEdit = (EditText) findViewById(R.id.freetext);
                    presenceInfo.setFreetext(freetextEdit.getText()
                            .toString());
                    EditText favoritelinkEdit = (EditText) findViewById(R.id.favoritelink);
                    presenceInfo.setFavoriteLinkUrl(favoritelinkEdit
                            .getText().toString());

                    // Publish the new presence info
                    if (presenceApi.setMyPresenceInfo(presenceInfo)) {
                        //cheat: to display toasts we should go back to the UI thread, however
                        //therefore we are using this construction
                        //NOTE: this is not a good practice, thought. The proper way to do this is to use handler.
                        PublishPresenceInfo.this
                                .runOnUiThread(new Runnable() {
                                    public void run() {
                                        Utils.displayToast(
                                                PublishPresenceInfo.this,
                                                getString(R.string.label_publish_ok));
                                    }
                                });

                    } else {
                        //cheat: to display toasts we should go back to the UI thread, however
                        //therefore we are using this construction
                        //NOTE: this is not a good practice, thought. The proper way to do this is to use handler.
                        PublishPresenceInfo.this    
                                .runOnUiThread(new Runnable() {
                                    public void run() {
                                        Utils.showMessageAndExit(
                                                PublishPresenceInfo.this,
                                                getString(R.string.label_publish_ko));
                                    }
                                });

                    }
                } catch (Exception e) {
                    //cheat: to display toasts we should go back to the UI thread, however
                    //therefore we are using this construction
                    //NOTE: this is not a good practice, thought. The proper way to do this is to use handler.
                    PublishPresenceInfo.this.runOnUiThread(new Runnable() {
                        public void run() {
                            Utils.showMessageAndExit(
                                    PublishPresenceInfo.this,
                                    getString(R.string.label_publish_ko));
                        }
                    });

                }
            }
        });

        thread.start();
    }
};



Original issue reported on code.google.com by vin...@broadcom.com on 16 Aug 2013 at 3:29

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

We don't test this part of the stack. Presence is optional and not deployed by 
Telco. We have no more presence server to test, so we are very interested if 
you can realize non regresion on this part and provide bug fixes in case of 
problems.

Original comment by jmauffret@gmail.com on 16 Aug 2013 at 8:39

  • Changed state: New

@GoogleCodeExporter
Copy link
Author

Hello,
Thank you for your answer.

What presence served did you used before? What happened to it?

I am not sure whether I can dedicate my time specifically to fixing this bug, 
however I did my best to outline the problem and identifying proposed fix, 
therefore implementing this change would not be a problem. 

Original comment by vin...@broadcom.com on 16 Aug 2013 at 9:15

@GoogleCodeExporter
Copy link
Author

Same as issue 139

Original comment by jmauffret@gmail.com on 19 Aug 2013 at 12:02

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant