我正在构建一个 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 转换为字符串时,为什么会出现错误?