Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Twitter post

For some reason I get an error when I try to display my twitter post in my app. I followed along with the videos and just changed the url from the treehouse blog to my twitter post url. I get some AsyncTask errors when I try to display the charArray in the log. It works with other urls so is my url bad? If so, is there a way to change it so that it does work?

6 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hi Domonique Dixon,

I'm sorry you are having this trouble! It looks like you came across an issue with the Content Length property that was logged in this other forum post. Please read through that post as I posted an alternative version of the doInBackground that should fix your problem.

I still need to either re-record or somehow address this issue in the context of the project. I'm sorry I have not done it yet as I've been focused on other priorities. Anyhow, hopefully this other solution helps you out!

First, to give you the best possible answer, please post your code.

Second, I don't believe you can use a charArray in a log. Convert your charArray to a String. Then try to log it. http://stackoverflow.com/questions/7655127/how-to-convert-a-char-array-back-to-a-string-java

protected String doInBackground(Object... arg0) {
            // Gets info from the http request
            int responseCode = -1;

            try {
                URL blogFeedUrl = new URL(facebookLink);
                HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
                connection.connect();

                responseCode = connection.getResponseCode();

                if(responseCode == HttpURLConnection.HTTP_OK) {
                    InputStream inputStream = connection.getInputStream();
                    Reader reader = new InputStreamReader(inputStream);

                    int contentLength = connection.getContentLength();
                    char[] charArray = new char[contentLength];

                    reader.read(charArray);
                    String responseData = new String(charArray);
                    Log.v(TAG, responseData);
                } else {
                    Log.i(TAG, "Unsuccessful HTTP Response Code: " + responseCode);
                }

                Log.i(TAG, "Code: " + responseCode);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                Log.i(TAG, "Wrong");

            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.i(TAG, "Wrong2");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.i(TAG, "Wrong3");
            }

            return "Code: " + responseCode;
        }

Did logging the String work?

i get an error on int contentLength = connection.getContentLength();

I'm sorry, I get -1 and a length

I'm going to try a work around.

Thanks for the help.