Problem with getting JSON to android app, propably problem with url

46
June 08, 2021, at 8:50 PM

I think i did something wrong with string named "url", but im not sure what should i change, what am i doing wrong?

I didn't want to share my api key, normally it is there. I use no commercial version of flickr.

Main activity:

                    FlickrFetchr flickrFetchr = new FlickrFetchr();
                    Log.d("HTTP_JSON",flickrFetchr.getJSONString());

JsonString is always "sth went wrong"

FlickrFetchr class:

public class FlickrFetchr {
    String API_KEY = "MY_API";
    HttpsURLConnection connection;
    public byte[] getUrlBytes(String urlSpec)
    {
        try {
            URL url = new URL(urlSpec);
           connection = (HttpsURLConnection) url.openConnection();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            InputStream imput = connection.getInputStream();
            if(connection.getResponseCode() != HttpsURLConnection.HTTP_OK) {
                throw new Exception();
            }
            int bytesRead;
            byte [] buffer = new byte[1024] ;
            do {
                bytesRead = imput.read(buffer);
                out.write(buffer,0,bytesRead);
            }while (imput.read(buffer)>0);
                out.close();
            return out.toByteArray();
        }catch (Exception e) {
            Log.e("HTTPeerpr", e.getMessage());
            byte[] emptyArray = new byte[0];
            return emptyArray;
        }
        finally {
            connection.disconnect();
        }
    }
   public String getURLString(String urlSpec)
    {
        return  getUrlBytes(urlSpec).toString();
    }
    public String getJSONString()
    {
        String jsonString = "sth went wrong";
        try {
            String url = Uri.parse("https://api.flickr.com/services/rest/")
                    .buildUpon()
                    .appendQueryParameter("method", "flickr.photos.getRecent")
                    .appendQueryParameter("api key", API_KEY)
                    .appendQueryParameter("format", "json")
                    .appendQueryParameter("nojsoncallback", "1")
                    .appendQueryParameter("extras", "url_s")
                    .build().toString();
            jsonString = getURLString(url);
        }catch (Exception je)
        {
            Log.e("JSON_ERROR", je.getMessage());
        }
            return jsonString;
    }
}

Thanks for your help.

READ ALSO
JavaScript heap out of memory? Now I can't even use npm?

JavaScript heap out of memory? Now I can't even use npm?

UPDATE: so, tried chancing memory size, now I can't even use "npm" commands at all to download new filesEverything throws a memory issue

54
base_url () function is not working ( Code ignitor)

base_url () function is not working ( Code ignitor)

im new to php and recently im trying to load my html and css file into the code ignitorThus, my website wont load when i try to link between pages using href, id be so grateful if anyone can point out my mistakes, thankyou

58
input date - preselect year without setting value?

input date - preselect year without setting value?

<input type="date" name="birthday" id="birthdate" required="">

23
Bootstrap-Table: Using Ajax call and add link to output table

Bootstrap-Table: Using Ajax call and add link to output table

I am using bootstrap table to insert data via an ajax callHowever, I cannot format this data in the table

44