【问题标题】:Android Instagram API: how to retrieve picturesAndroid Instagram API:如何检索图片
【发布时间】:2014-06-16 18:42:52
【问题描述】:

我正在构建一个 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 按 Authorize 后,AuthWebViewClient 成功从 Instagram 获取 request_token。

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

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

先打印“before streamToString”,然后打印“ERROR AsyncTask”,永远不会到达“after streamToString”。

我正在通过单击按钮启动 LongOperation:

doSomethingIunno.setOnClickListener(new View.OnClickListener() {

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

    });

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

【问题讨论】:

    标签: android webview inputstream instagram httpsurlconnection


    【解决方案1】:

    好吧,与其构建自己的代码,不如尝试使用library 来代替?查看“instagram get feeds”部分的 custom-ui 示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多