bettybop68 / android-rcs-ims-stack

Automatically exported from code.google.com/p/android-rcs-ims-stack
1 stars 1 forks source link

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

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
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 commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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 commented 8 years ago
Same as issue 139

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