Search code examples
javaandroidinstagram-api

Getting 'filenotfound' exception on an Instagram hashtag search query


I am trying to run a program which search Instagram for pictures of a specific hashtag. Below is my code for the program. When I am running the program, it is not showing any error, but it is returning "filenotfoundexception" from Instagram. I think it is in the endpoint URL, but I can't exactly figure out where I am making the mistake. How do I figure it out?

public class SearchActivity extends Activity {

    public EditText edtSearch;
    private final String INSTA_CONS_ID = "xxxx";
    private final String INSTA_CONS_SEC_KEY = "xxxx";
    public static final String APIURL = "https://api.instagram.com/v1/tags";
    ListView list;
    public int index;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        Button btnInsta =(Button)findViewById(R.id.btnInsta);
        edtSearch =(EditText)findViewById(R.id.edtSearch);
        list = (ListView)findViewById(R.id.list);

        btnInsta.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                new SearchOnInstagram().execute(edtSearch.getText().toString());
            }
        });
    }

    class SearchOnInstagram extends AsyncTask<String, Void, Integer> {
        final List<Instagram> inst = new ArrayList<Instagram>();
        final int SUCCESS = 0;
        final int FAILURE = SUCCESS + 1;
        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = ProgressDialog.show(SearchActivity.this, "", getString(R.string.searchinginsta));
        }

        @Override
        protected Integer doInBackground(String... params) {
            try {

                Query query = new Query(params[0]);
                String urlString = APIURL + "/"+ query +"/media/recent?client_id=" + INSTA_CONS_ID;
                        URL url = new URL(urlString);

                InputStream inputStream = url.openConnection().getInputStream();
                String response = streamToString(inputStream);
                JSONObject jsonObject = (JSONObject) new JSONTokener(response).nextValue();
                JSONArray jsonArray = jsonObject.getJSONArray("data");

                for (int i =0; i<= jsonArray.length(); i++) {
                    JSONObject mainImageJsonObject = jsonArray.getJSONObject(index).getJSONObject("images").getJSONObject("standard_resolution");
                    String imageUrlString = mainImageJsonObject.getString("url");
                    String userName = "@Arnab";

                    Instagram instItem = new Instagram(userName,imageUrlString);
                    inst.add(instItem);
                }
                return SUCCESS;
            }
            catch (Exception e) {
                e.printStackTrace();
            }

            return FAILURE;
        }

        @Override
        protected void onPostExecute(Integer result)
        {
            super.onPostExecute(result);
            dialog.dismiss();
            if (result == SUCCESS) {
                list.setAdapter(new InstagramAdapter(SearchActivity.this, inst));
            }
            else
            {
                Toast.makeText(SearchActivity.this, getString(R.string.error), Toast.LENGTH_LONG).show();
            }
        }

        public static String streamToString(InputStream p_is)
        {
            try
            {
                BufferedReader m_br;
                StringBuffer m_outString = new StringBuffer();
                m_br = new BufferedReader(new InputStreamReader(p_is));
                String m_read = m_br.readLine();
                while(m_read != null)
                {
                    m_outString.append(m_read);
                    m_read =m_br.readLine();
                }
                return m_outString.toString();
            }
            catch (Exception p_ex)
            {
                p_ex.printStackTrace();
                return "";
            }
        }

I am getting the following exception

09-09 16:20:10.070  25571-25625/com.example.smarthelp W/System.err?       java.io.FileNotFoundException:  https://api.instagram.com/v1/tags/Query{query='india', lang='null', locale='null', maxId=-1, count=-1, since='null', sinceId=-1, geocode='null', until='null', resultType='null', nextPageQuery='null'}/media/recent?client_id=xxx
09-09 16:20:10.070  25571-25625/com.example.smarthelp W/System.err? at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
09-09 16:20:10.070  25571-25625/com.example.smarthelp W/System.err? at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
09-09 16:20:10.070  25571-25625/com.example.smarthelp W/System.err? at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
09-09 16:20:10.070  25571-25625/com.example.smarthelp W/System.err? at com.example.smarthelp.SearchActivity$SearchOnInstagram.doInBackground(SearchActivity.java:156)

Solution

  • What is the Query class / object?

    Your URL is mal-formed.

        Query query = new Query(params[0]);
        String urlString = APIURL + "/"+ query +"/media/recent?client_id=" + INSTA_CONS_ID;
                URL url = new URL(urlString);
    

    Should instead be

        String urlString = APIURL + "/"+ params[0] +"/media/recent?client_id=" + INSTA_CONS_ID;
                URL url = new URL(urlString);
    

    But there must be a reason the Query object is used. So the above is not the most elegant way, but it should work.