URL::readEntireTextStream();


#1

is

URL::readEntireTextStream();

working on Android ?

Cant make it work, works in ios simulator but not on android device.

Is it just me?


#2

That method is platform-independent, so will work the same anywhere. The underlying URL functionality should work on Android (I think it just uses the same http classes as for linux) - if you're having trouble, we'd need to know more about exactly where it's failing, as the process of reading a URL is pretty complex.


#3

Hi

Thank you for getting back to me on this.

Yes I see that readEntireTextStream is just a toplevel function calling readEntireTextStream::readEntireTextStream()

To test, I have now created a new fresh project with the latest Introjucer, from the git-tip.

It is the basic setup with “Create a basic window”

Then I have added a TextEditor in the constructor and attempt to set it’s text from the url result.

Here is the MainComponent.h


/*
  ==============================================================================
    This file was auto-generated!
  ==============================================================================
*/
#ifndef MAINCOMPONENT_H_INCLUDED
#define MAINCOMPONENT_H_INCLUDED
#include "../JuceLibraryCode/JuceHeader.h"

//==============================================================================
/*
    This component lives inside our window, and this is where you should put all
    your controls and content.
*/
class MainContentComponent   : public Component
{
public:
    //==============================================================================
    MainContentComponent();
    ~MainContentComponent();
    void paint (Graphics&);
    void resized();
private:
    //==============================================================================
    ScopedPointer<TextEditor> textEditor;
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
};

#endif  // MAINCOMPONENT_H_INCLUDED

And here is the MainComponent.cpp

 


/*

This file was auto-generated!

==============================================================================
*/

#include “MainComponent.h”

//==============================================================================
MainContentComponent::MainContentComponent()
{
//Create TextEditor
addAndMakeVisible(textEditor = new TextEditor(“test”));

//Set Size
juce::Rectangle&lt;int&gt; screenArea = Desktop::getInstance().getDisplays().getMainDisplay().userArea;
setSize(screenArea.getWidth(), screenArea.getHeight());

//Get text from the web
URL theUrl("http://audiostrom.com/hello.txt");
String fromWeb = theUrl.readEntireTextStream();
textEditor-&gt;setText(fromWeb,dontSendNotification);

}

MainContentComponent::~MainContentComponent()
{
}

void MainContentComponent::paint (Graphics& g)
{
g.fillAll (Colour (0xff001F36));

g.setFont (Font (16.0f));
g.setColour (Colours::white);
g.drawText ("Hello World!", getLocalBounds(), Justification::centred, true);

}

void MainContentComponent::resized()
{
// This is called when the MainContentComponent is resized.
// If you add any child components, this is where you should
// update their positions.
textEditor->setBounds(0, 0, getWidth(), 25);
}

 

When running in the iOS simulator the TextBox gets the text from the url, but when I compile using terminal, ant release, upload to my android device. The textbox does not get any text.

How do you debug under android, do I need to use the android emulators and eclipse or Android Studio in some way? Until now I have just written the code in xCode and compiled with ant.

 

 


#4

I can confirm it has worked in the past (see my unit tests at http://www.juce.com/forum/topic/suggested-code-handling-url-redirects).

It's been months since I last played with android and I can't remember much, but I did use ant debug and didn't get along with Eclipse.


#5

One thought: did you make sure that your app has permission for network access?


#6

Andrew, Thanks for comfirming that this should work, but I can't see that I'm doing anything special in the code abbove.

The Introjucer sets the Internet Access flag by default, and in the AndroidManifest for the app I can see 
android.permission.INTERNET

There should no be much more to it?


#7

FYI, I confirm this code works on an iPad Mini and not on a Nexus 10 (Android 4.4.4).

Maybe it should be on a separate thread ? I think that Android is not very keen on allowing synchronous calls in the main thread.


#8

Just looking at your code snippet, you really should do this on a background thread. By putting the code in the constructor it's probably getting called before the app has even finished launching.


#9

Yeah.. that was to keep the example super simple.. ofcourse my real app is not like this.

But let's say i move the code to a button click callback, same result.

Or are you really saying that this woun't work in the message thread? 

I will try it on a background thread an see if that works. If it does then we know that the url class cant be used on the main thread on Android 

(But in a real world app, you would probably want it on a sepperate thread like Jules says)


#10

I'll try it on a thread


#11

Ah. Yes, that's it - I just remembered that on Android the message thread is not allowed to do any network access.

But the code contains an assertion to detect this and warn you - maybe you were running a release build..


#12

Aha!

Thats probably it then. Lets put this in the docs and call it case closed ?


#13

I thought it would be in the docs, but apparently not - I'll add a note about it!