代码之家  ›  专栏  ›  技术社区  ›  Arnab

在Instagram标签搜索查询中获取“filenotfound”异常

  •  0
  • Arnab  · 技术社区  · 10 年前

    我正在尝试运行一个程序,在Instagram上搜索特定标签的图片。以下是我的程序代码。当我运行该程序时,它没有显示任何错误,但从Instagram返回“filenotfoundexception”。我认为它在端点URL中,但我无法确切地找出我在哪里出错。怎么做 我明白了吗?

    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 "";
                }
            }
    

    我遇到了以下异常

    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)
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Shiva    10 年前

    什么是 Query 类/对象?

    您的URL格式错误。

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

    应改为

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

    但一定有原因 查询 对象。因此,上述方法不是最优雅的方法,但它应该有效。