2014-06-16 44 views
1

我正在構建一個Android應用程序,該應用程序使用Instagram API檢索Instagram圖像,然後將其顯示在我的應用程序中。Android Instagram API:如何檢索圖片

我一直在努力使用我在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轉換爲字符串時出現錯誤!

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之前」,然後「錯誤AsyncTask」,從不到達「在streamToString之後」。

我開始與一個按鈕點擊LongOperation:

doSomethingIunno.setOnClickListener(new View.OnClickListener() { 

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

    }); 

這裏有什麼錯?當我嘗試將InputStream轉換爲字符串時,爲什麼會出現錯誤?

回答

3

那麼,你爲什麼不嘗試用這個library來代替構建你自己的代碼呢?查看「instagram get feeds」部分的custom-ui示例。