Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Masum Bergmann
PLUS
Masum Bergmann
Courses Plus Student 4,129 Points

Retrieving (Text)File from Parse.com using getDataInBackground() does not work!

Hi all!

I'm working on the Ribbit project and have implemented a text sending and receiving feature. To retrieve the text file I have used the following code:

file.getDataInBackground(new GetDataCallback() {

    @Override
    public void done(byte[] data, ParseException e) {

        if (e == null)
        {
            mTextMsg = new String(data);                        
        }
        else
        {
            Log.e("InboxFrag","e: " + e);
        }

    }
});

However this does not return anything into mTextMsg. I have used the debug mode and found out that after this code runs, mTextMsg remains null. Why is that? Am I doing something wrong? My guess would be yes, but I can't see what.

I have then tried the "not so recommended" way of calling getData() instead:

try {
    mTextMsg = new String(file.getData());

        } catch (ParseException e) {

    Log.e("InboxFrag","e: " + e);
    }

This actually works, but the downside is that data retrieval is not done in the Background.

I have no idea why the getDataInBackground() method does not work! (SOOOOO FRUSTRATING!!) Any help figuring this out would be very much appreciated... Thanks!!

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

With the first method, what do the data and e variables hold when you step into the done() method?

Masum Bergmann
Masum Bergmann
Courses Plus Student 4,129 Points

Hi Ben, sorry for the delayed response, but I was on holiday in Spain with rather limited access to the internet!

data actually contains the text information and looks like : 72, 101, 108, 108, 111 which I'm guessing is ok since data is a byte array.

e is null, which I suppose is also good news!

Does this mean that there may be a problem converting the byte array into a string?

Thanks for your help!!

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hmmm...I'm not sure why your data isn't being converted. Perhaps it has something to do with how the array is being stored: http://stackoverflow.com/a/6684822/475217

However, I'm wondering if it would be easier and more efficient to store the text message as simply a new text column rather than putting it into a file.

Masum Bergmann
Masum Bergmann
Courses Plus Student 4,129 Points

Cheers Ben, I'll keep playing around with it a bit more and will let you know if I find anything!