gpt4 book ai didi

Android Instagram API : how to retrieve pictures

转载 作者:行者123 更新时间:2023-11-29 17:49:16 24 4
gpt4 key购买 nike

我正在构建一个 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) {
}
}

我想知道 token 变量是什么? (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

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

我通过单击按钮启动 LongOperation:

doSomethingIunno.setOnClickListener(new View.OnClickListener() {

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

});

这是怎么回事?为什么在我尝试将 InputStream 转换为字符串时出现错误?

最佳答案

好吧,与其构建自己的代码,不如尝试使用这个 library反而 ?查看“instagram get feeds”部分的自定义 ui 示例。

关于Android Instagram API : how to retrieve pictures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24250119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com