RE: Can't get supplied code to work key problem


#1

Hi,
I coded this up myself in the first instance and ended up with an error code 100, invalid API Key…(Key has invalid format. …
Couldn’t find an error in my code so downloaded example code from site. Same error. I then saw on the Flickr site that they had changed from http to https and so I tried making those changes to the code. A little unsure of what was needed exactly but here’s what I did:


package com.bignerdranch.android.photogallery;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;

import javax.net.ssl.HttpsURLConnection;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.net.Uri;
import android.util.Log;

public class FlickrFetchr {
public static final String TAG = “PhotoFetcher”;
private static final String ENDPOINT = “https://api.flickr.com/services/rest/”;
private static final String API_KEY = “91aee5e20f7e9ce960518e30942e7e5”;
private static final String METHOD_GET_RECENT = “flickr.photos.getRecent”;
private static final String PARAM_EXTRAS = “extras”;

private static final String EXTRA_SMALL_URL = "url_s";

private static final String XML_PHOTO = "photo";

public byte[] getUrlBytes(String urlSpec) throws IOException {
    URL url = new URL(urlSpec);
    HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();

    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream in = connection.getInputStream();
        
        if (connection.getResponseCode() != HttpsURLConnection.HTTP_OK) {
            return null;
        }

        int bytesRead = 0;
        byte[] buffer = new byte[1024];
        while ((bytesRead = in.read(buffer)) > 0) {
            out.write(buffer, 0, bytesRead);
        }
        out.close();
        return out.toByteArray();
    } finally {
        connection.disconnect();
    }
}

(Where ever it appears, https replaces http and the appropriate import for the HttpsURLConnection.
This doesn’t work either (same error). Can anybody help me with this?

Cheers

Brian


#2

Hi,
I just changed ENDPOINT from http to https, it worked fine.
Maybe no need use HttpsURLConnection.
regards.
carl


#3

See this
http://forums.bignerdranch.com/viewtopic.php?f=423&t=8944