Android Instagram API:如何检索图片

时间:2014-06-16 18:42:53

标签: android webview inputstream instagram httpsurlconnection

我正在构建一个Android应用程序,它使用Instagram API检索Instagram图像,然后在我的应用程序中显示它们。

我一直在尝试使用我在this上找到的唯一教程来使其工作, 与this相同。

我已经能够通过在Webview中加载Instagram身份验证来实现第一部分了,但是我遇到了第二部分的问题,即通过获取Instagram imageUrl实际上从我的Instagram帐户获取图像。

具体来说,我遇到了这个部分的问题:

class LongOperation extends AsyncTask<String, Void, String> {

static String accessTokenString, id, username, urlString, imageUrlString;

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

   try {
   URL url = new URL(tokenURLString);
   HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
   httpsURLConnection.setRequestMethod("POST");
   httpsURLConnection.setDoInput(true);
   httpsURLConnection.setDoOutput(true);
   OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpsURLConnection.getOutputStream());
   outputStreamWriter.write("client_id="+client_id+
                                    "client_secret="+ client_secret +
                                    "grant_type=authorization_code" +
                                    "redirect_uri="+CALLBACKURL+
                                    "code=" + token);
   outputStreamWriter.flush();

   Log.i(TAG, "before streamToString");       

   String response = streamToString(httpsURLConnection.getInputStream());

   Log.i(TAG, "after streamToString");

   JSONObject jsonObject = (JSONObject) new JSONTokener(response).nextValue();

   accessTokenString = jsonObject.getString("access_token"); //Here is your ACCESS TOKEN
   id = jsonObject.getJSONObject("user").getString("id");
   username = jsonObject.getJSONObject("user").getString("username"); 
   //This is how you can get the user info. 
   //You can explore the JSON sent by Instagram as well to know what info you got in a response
    }
    catch (Exception e)
    {
        Log.e(TAG, "ERROR AsyncTask");
    }

    return null;
    }

    //converts Stream to String
    public 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();
             }

             Log.d(TAG, "m_outString: " + m_outString.toString());
            return m_outString.toString();
       }
       catch (Exception e)
       {
           Log.e(TAG, "ERROR streamToString");
       }

       return null;
    }

    @Override
    protected void onPostExecute(String result) {
    Log.d(TAG, "Executed AsyncTask");
    }

        @Override
    protected void onPreExecute() {
        Log.d(TAG, "About to execute AsyncTask");
    }

        @Override
    protected void onProgressUpdate(Void... values) {
    }
}   

我想知道令牌变量是什么? (doInBackground方法,在outStreamWriter参数中)

我目前正在从AuthWebViewClient发出request_token。 在我的WebView中按“授权”后,AuthWebViewClient成功从Instagram获取request_token。

但是在尝试将InputStream转换为String时出现错误!

06-16 14:14:42.302: D/tellmeaboutit(31244): About to execute AsyncTask
06-16 14:14:42.642: I/tellmeaboutit(31244): request_token: 235958nvzdj243u9o974jd1490139238
06-16 14:14:42.642: I/tellmeaboutit(31244): before streamToString
06-16 14:14:42.792: D/tellmeaboutit(31244): ERROR AsyncTask

在“streamToString之前”打印然后“ERROR AsyncTask”并且永远不会到达“在streamToString之后”。

我点击按钮启动LongOperation:

doSomethingIunno.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            new LongOperation().execute("");
        }

    });

这里有什么问题?当我尝试将InputStream转换为String时,为什么会出现错误?

1 个答案:

答案 0 :(得分:3)

那么,为什么不尝试使用此library而不是构建自己的代码?查看&#34; instagram获取Feed&#34;上的custom-ui示例部分。