从Flickr JSON获取图像URL

时间:2014-12-09 19:13:46

标签: android json flickr

我无法通过Flickr API获取图片的网址。我正在为Instagram做类似的事情并且它正在工作但是当我的Flickr活动加载时,什么都没有出现。加载了JSON,但问题似乎是获取图像的URL。知道我做错了吗?

public class FlickrActivity extends BaseActivity {

    public static GridView gridView;
    public static GridViewAdapter gridViewAdapter;

    static String url;

    static ArrayList<String> picURLS;

    static Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid);

        gridView = (GridView)findViewById(R.id.imageGrid);
        gridViewAdapter = new GridViewAdapter(this);
        // gridView.setAdapter(gridViewAdapter);

        context = getApplicationContext();

        getActionBar().setTitle("Flickr");

        url = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=API KEY&per_page=50&user_id=52540720@N02&format=json&nojsoncallback=1";

        picURLS = new ArrayList<String>();

        new ParseJSON().execute();

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(FlickrActivity.this, ImageActivity.class);
                intent.putExtra("position", position);
                startActivity(intent);
            }
        });
    }

    public static class JSONParser {
        static InputStream is = null;
        static JSONObject jObj = null;
        static String json = "";

        // constructor
        public JSONParser()
        {

        }

        public JSONObject getJSONFromUrl(String jsonUrl)
        {
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(jsonUrl);
            String responseBody = "DEFAULT_MSG_TEXT";
            int resCode = 0;

            try{

                HttpResponse response = client.execute(get);

                int responseCode = response.getStatusLine().getStatusCode();
                resCode = responseCode;

                switch(responseCode) {
                    case 200:
                        HttpEntity entity = response.getEntity();
                        if(entity != null) {
                            responseBody = EntityUtils.toString(entity);
                        }
                        break;
                }
            }
            catch(Exception ex){
                Log.e("Post Error",resCode + "\n Exception" + ex);
                responseBody = "DEFAULT_MSG_TEXT";
            }

            json = responseBody;

            // try to parse the string to a JSON object
            try
            {
                jObj = new JSONObject(json);
            }
            catch (JSONException e)
            {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }

            // return JSON String
            return jObj;
        }
    }

    public static class ParseJSON extends AsyncTask<Void,Void,ArrayList> {
        @Override
        protected void onPreExecute()
        {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

        @Override
        protected ArrayList doInBackground(Void... params) {
            JSONParser jParser = new JSONParser();

            // get json from url here
            JSONObject json = jParser.getJSONFromUrl(url);
            System.out.println("JSON: " + json);

            try {
                // JSONArray dataArray = json.getJSONArray("photo");
                JSONObject photos = json.getJSONObject("photos");
                JSONArray dataArray = photos.getJSONArray("photo");
                int thumbnailsCount = dataArray.length();

                for (int i = 0; i < thumbnailsCount; i++) {
                    JSONObject farm = dataArray.getJSONObject(i).getJSONObject("farm");
                    JSONObject server = dataArray.getJSONObject(i).getJSONObject("server");
                    JSONObject id = dataArray.getJSONObject(i).getJSONObject("id");
                    JSONObject secret = dataArray.getJSONObject(i).getJSONObject("secret");
                    String picURL = String.format("http://farm%s.static.flickr.com/%s/%s_%s_b.jpg", farm, server, id, secret);
                    System.out.println(picURL);
                    picURLS.add(picURL);
                }
            }
            catch (Exception e) {
                e.getMessage().toString();
            }

            return picURLS;
        }

        @Override
        protected void onPostExecute(ArrayList result) {
            super.onPostExecute(result);
            gridView.setAdapter(gridViewAdapter);

            for (String thumb : picURLS) {
                System.out.println(thumb);
            }
        }
    }

    public class GridViewAdapter extends ArrayAdapter {

        Context context;

        public GridViewAdapter(Context context) {
            super(context, 0);
            this.context = context;
        }

        public int getCount() {
            return picURLS.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View row = convertView;
            ViewHolder holder;

            if(row == null) {
                LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                row = inflater.inflate(R.layout.grid_row, parent, false);

                holder = new ViewHolder();
                holder.imageView = (ImageView)row.findViewById(R.id.gridImageView);

                // ImageView gridImageView = (ImageView)row.findViewById(R.id.gridImageView);

                row.setTag(holder);
            }
            else {
                holder = (ViewHolder) row.getTag();
            }

            Picasso.with(context)
                    .load(picURLS.get(position))
                    .placeholder(R.drawable.placeholder)
                    .error(R.drawable.placeholder)
                    .into(holder.imageView);

            return row;
        }
    }

    static class ViewHolder {
        ImageView imageView;
    }
}

这是JSON:

{"photos":{"page":1,"pages":254,"perpage":50,"total":"12665","photo":[{"id":"15355145634","owner":"52540720@N02","secret":"a142bd54b5","server":"8580","farm":9,"title":"christmas-scott","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15356461163","owner":"52540720@N02","secret":"8508527108","server":"8623","farm":9,"title":"Jackson Tolle","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15774323510","owner":"52540720@N02","secret":"1a69e1bfa6","server":"7476","farm":8,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15935827796","owner":"52540720@N02","secret":"f2925ac043","server":"8650","farm":9,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15774324280","owner":"52540720@N02","secret":"48fc882459","server":"8639","farm":9,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15775880047","owner":"52540720@N02","secret":"81ec9b78ab","server":"7485","farm":8,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15775585989","owner":"52540720@N02","secret":"c5eca94b67","server":"8639","farm":9,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15961599425","owner":"52540720@N02","secret":"87093030fd","server":"8627","farm":9,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15774200278","owner":"52540720@N02","secret":"74ed5b5bda","server":"7554","farm":8,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15775880667","owner":"52540720@N02","secret":"e4a2105e0e","server":"7560","farm":8,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15339327134","owner":"52540720@N02","secret":"7545e69058","server":"7505","farm":8,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15774325070","owner":"52540720@N02","secret":"d84e6cd95a","server":"8680","farm":9,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15341979093","owner":"52540720@N02","secret":"a30e8f10c6","server":"7541","farm":8,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15961598125","owner":"52540720@N02","secret":"53ceb7f600","server":"8591","farm":9,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15775881157","owner":"52540720@N02","secret":"ddd1bf3ab5","server":"7501","farm":8,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15959628961","owner":"52540720@N02","secret":"45daeb449f","server":"7557","farm":8,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15960946592","owner":"52540720@N02","secret":"dea17cf59e","server":"7583","farm":8,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15961604775","owner":"52540720@N02","secret":"95eb392b5b","server":"7480","farm":8,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15775590449","owner":"52540720@N02","secret":"bb419500e6","server":"7557","farm":8,"title":"2013-14 LEAD graduating class","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15341959493","owner":"52540720@N02","secret":"e1af801876","server":"7492","farm":8,"title":"Mr. Melton introduces Mr. Haney at General Session","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15960927522","owner":"52540720@N02","secret":"afaa266aa9","server":"7563","farm":8,"title":"Mr. Haney Mr. Beck Mr. Melton","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15339308454","owner":"52540720@N02","secret":"91ec731e93","server":"7506","farm":8,"title":"Mr. Haney - General Session","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15341959853","owner":"52540720@N02","secret":"2e06aa9460","server":"7565","farm":8,"title":"General Session - Mr. Haney","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15774182448","owner":"52540720@N02","secret":"15aae90810","server":"7544","farm":8,"title":"FFCLA Pres","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15775862227","owner":"52540720@N02","secret":"03c0f94763","server":"8569","farm":9,"title":"FFA President","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15774307010","owner":"52540720@N02","secret":"f7acc992d6","server":"7520","farm":8,"title":"Carl Hurley - humorist","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15774065278","owner":"52540720@N02","secret":"3b95230951","server":"7515","farm":8,"title":"2014 \"Top County\" - Butler County Farm Bureau","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15341787543","owner":"52540720@N02","secret":"5688437582","server":"7499","farm":8,"title":"230","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15341789423","owner":"52540720@N02","secret":"e15292294c","server":"7574","farm":8,"title":"229","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15774134430","owner":"52540720@N02","secret":"de2435b657","server":"7530","farm":8,"title":"228","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15960758462","owner":"52540720@N02","secret":"283dce0a2d","server":"8586","farm":9,"title":"227","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15961411555","owner":"52540720@N02","secret":"6b69560354","server":"7576","farm":8,"title":"226","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15339139634","owner":"52540720@N02","secret":"50b0106bea","server":"7558","farm":8,"title":"225","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15339140194","owner":"52540720@N02","secret":"939177dfba","server":"7581","farm":8,"title":"224","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15775402169","owner":"52540720@N02","secret":"b0efef29bf","server":"8673","farm":9,"title":"223","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15341794043","owner":"52540720@N02","secret":"612e340f7c","server":"7504","farm":8,"title":"222","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15960762422","owner":"52540720@N02","secret":"ac059f3245","server":"7525","farm":8,"title":"221","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15959446971","owner":"52540720@N02","secret":"58a59d06d8","server":"7574","farm":8,"title":"220","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15960763572","owner":"52540720@N02","secret":"114120a5be","server":"7581","farm":8,"title":"219","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15774141340","owner":"52540720@N02","secret":"93a6471dd7","server":"7482","farm":8,"title":"218","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15961417175","owner":"52540720@N02","secret":"e6ed2869d0","server":"7509","farm":8,"title":"217","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15935642466","owner":"52540720@N02","secret":"3399f43c92","server":"7514","farm":8,"title":"216","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15775407129","owner":"52540720@N02","secret":"887382ab10","server":"7581","farm":8,"title":"215","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15960766212","owner":"52540720@N02","secret":"c7e230b59a","server":"8633","farm":9,"title":"214","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15774144000","owner":"52540720@N02","secret":"2bd9723772","server":"7557","farm":8,"title":"213","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15959451401","owner":"52540720@N02","secret":"836b23ea2b","server":"8567","farm":9,"title":"212","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15774145230","owner":"52540720@N02","secret":"bb08df2de2","server":"7530","farm":8,"title":"211","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15339149004","owner":"52540720@N02","secret":"baf8e95075","server":"7512","farm":8,"title":"210","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15339149524","owner":"52540720@N02","secret":"9b8eddb494","server":"8591","farm":9,"title":"209","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15935647536","owner":"52540720@N02","secret":"00aa323a5a","server":"7515","farm":8,"title":"208","ispublic":1,"isfriend":0,"isfamily":0}]},"stat":"ok"}

看起来我没有获得农场,服务器,ID和秘密来形成每个网址,这解释了为什么没有图像出现。得到那些我做错了什么?

2 个答案:

答案 0 :(得分:0)

farm,server,id和secret不应该是JSONObjects。它们应该是字符串,所以它应该是  String farm = dataArray.getJSONObject(i).getString("farm");

答案 1 :(得分:0)

您获得了服务器,服务器,ID和密码,但它们处于第三级。你需要查看照片&gt;照片&gt;服务器场(或服务器,ID等)。获得该数据后,即可创建网址。 Check out this page by Flickr for more details.