-1


Having a problem getting an app I'm building to connect to the Internet. I've built in the ability for it to check that there is a network and that it is a wifi network before it attempts to connect to the Internet and get data from an RSS feed.

I'm running the app on the Android player installed on a BlackBerry PlayBook and a BlackBerry Z10. The PlayBook behaves as expected, it gets the feed fine. The Z10 just throws exceptions if it manages to clear the error checking, though it behaves as expected if there isn't a network available.

I should mention that the app has the relevant permissions in its Manifest file, though for some reason only the PlayBook bothered to show me a permissions screen when I ran the program through Android Studio's debug and had adb pass the program along to the BlackBerry ADB Proxy Manager. It also only bothered to do that after about three debug runthroughs.

Completely at a loss as to what I should try next. Anyone got any thoughts?

Edit: In case it helps, here's the code that I added to a sample FullScreen Activity app from Android Studio in the onCreate method:

ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    Boolean isConnected = activeNetwork != null &&
            activeNetwork.isConnectedOrConnecting(); // Check for the existence of a network connection
    String helloString;
    if (isConnected == true) {
        // There is a network connection
        Log.i("helloHeadlines", "FullscreenActivity.onCreate - there is network available");
        //helloString = headlines_feed.connectionStatus();
        Boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI ;
        if (isWiFi == true) {
            // There is WiFi - we may try to use the Internet
            headlines headlinesS = new headlines();
            try {
                helloString = headlinesS.headlines_stream()[0][0];
            } catch (Exception e) {
                helloString = "Error: Received an exception";
            }
            //helloString = "There is WiFi available";
        } else {
            helloString = "There is no WiFi available";
        }
    } else {
        // There is not a network connection
        helloString = "There is no network connection";
    }
    final TextView mTextView = (TextView) findViewById(R.id.fullscreen_content);
    mTextView.setText(helloString);

I don't think the problem is with my headlines class because it runs fine on both the PlayBook and on Eclipse when I run it into the console window.

Edit 2: Think I might have found the source of the problem. Changed one line of code and got a new error message:

} catch (Exception e) {
       helloString = "Error: Received an exception";
}

became

} catch (Exception e) {
       helloString = e.toString();
}

which gave the result

android.os.NetworkOnMainThreadException

ReesA
  • 1
  • 1

1 Answers1

0

Solved it. Went and did a search for the exception and found the answer in this thread: networkonmainthread. Had to do a bit of reworking because the new thread was not pushing its results into my TextView properly. Ended up with the following code:

private String helloString;
private void setText() {
    final TextView mTextView = (TextView) findViewById(R.id.fullscreen_content);
    Log.i("helloHeadlines", "FullscreenActivity.onCreate - helloString equals " + helloString);
    mTextView.setText(helloString);
}

Thread thread = new Thread(new Runnable(){
    @Override
    public void run() {
        headlines headlinesS = new headlines();
        try {
            helloString = headlinesS.headlines_stream()[0][0];
            Log.i("helloHeadlines", "FullscreenActivity.thread.run - set helloString to " + helloString);
        } catch (Exception e) {
            helloString = e.toString();
            Log.i("helloHeadlines", "FullscreenActivity.thread.run - " + e.toString());
        }
        Log.i("helloHeadlines", "FullscreenActivity.thread.run - exiting try loop");
        setText();
    }
});

and the following inside the onCreate method:

ConnectivityManager cm =
        (ConnectivityManager)     getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
Boolean isConnected = activeNetwork != null &&
        activeNetwork.isConnectedOrConnecting(); // Check for the existence of a network connection

if (isConnected == true) {
    // There is a network connection
    Log.i("helloHeadlines", "FullscreenActivity.onCreate - there is network available");
    //helloString = headlines_feed.connectionStatus();
    Boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI ;
    if (isWiFi == true) {
        // There is WiFi - we may try to use the Internet
        thread.start();
        Log.i("helloHeadlines", "FullscreenActivity.onCreate - exited thread.");
        //helloString = "There is WiFi available";
    } else {
        helloString = "There is no WiFi available";
    }
} else {
    // There is not a network connection
    helloString = "There is no network connection";
}
setText();
Community
  • 1
  • 1
ReesA
  • 1
  • 1