gpt4 book ai didi

java - 如何解决 com.google.gson.JsonException : java IllegalStateException :Expected BEGIN_OBJECT but was String at line 1 coulmn 10

转载 作者:行者123 更新时间:2023-12-01 11:55:33 25 4
gpt4 key购买 nike

我从引用的站点获取了此代码,用于在列表中获取 Twitter 提要,但我收到了如上所述的 JsonException 错误请需要一些帮助来解决该错误

public class TwitterAPI {
private String twitterApiKey;
private String twitterAPISecret;
final static String TWITTER_TOKEN_URL =
"https://api.twitter.com/oauth2/token";
final static String TWITTER_STREAM_URL =
"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=";

public TwitterAPI(String twitterAPIKey, String twitterApiSecret) {
this.twitterApiKey = twitterAPIKey;
this.twitterAPISecret = twitterApiSecret;
}

public ArrayList<TwitterTweet> getTwitterTweets(String screenName) {
ArrayList<TwitterTweet> twitterTweetArrayList = null;
try {
String twitterUrlApiKey = URLEncoder.encode(twitterApiKey, "UTF-8");
String twitterUrlApiSecret = URLEncoder.encode(twitterAPISecret,
"UTF-8");
String twitterKeySecret = twitterUrlApiKey + ":" +
twitterUrlApiSecret;
String twitterKeyBase64 =
Base64.encodeToString(twitterKeySecret.getBytes(), Base64.NO_WRAP);
TwitterAuthToken twitterAuthToken =
getTwitterAuthToken(twitterKeyBase64);
twitterTweetArrayList = getTwitterTweets(screenName,
twitterAuthToken);
} catch (UnsupportedEncodingException ex) {
} catch (IllegalStateException ex1) {
}
return twitterTweetArrayList;
}

public ArrayList<TwitterTweet> getTwitterTweets(String screenName,
TwitterAuthToken twitterAuthToken) {
ArrayList<TwitterTweet> twitterTweetArrayList = null;
if (twitterAuthToken != null &&
twitterAuthToken.token_type.equals("bearer")) {
HttpGet httpGet = new HttpGet(TWITTER_STREAM_URL + screenName);
httpGet.setHeader("Authorization", "Bearer " +
twitterAuthToken.access_token);
httpGet.setHeader("Content-Type", "application/json");
HttpUtil httpUtil = new HttpUtil();
String twitterTweets = httpUtil.getHttpResponse(httpGet);
twitterTweetArrayList = convertJsonToTwitterTweet(twitterTweets);
}
return twitterTweetArrayList;
}

public TwitterAuthToken getTwitterAuthToken(String twitterKeyBase64) throws
UnsupportedEncodingException {
HttpPost httpPost = new HttpPost(TWITTER_TOKEN_URL);
httpPost.setHeader("Authorization", "Basic " + twitterKeyBase64);
httpPost.setHeader("Content-Type", "application/x-www-form-
urlencoded;charset=UTF-8");
httpPost.setEntity(new StringEntity("grant_type=client_credentials"));
HttpUtil httpUtil = new HttpUtil();
String twitterJsonResponse = httpUtil.getHttpResponse(httpPost);
return convertJsonToTwitterAuthToken(twitterJsonResponse);
}

private TwitterAuthToken convertJsonToTwitterAuthToken(String jsonAuth) {
TwitterAuthToken twitterAuthToken = null;
if (jsonAuth != null && jsonAuth.length() > 0) {
try {
Gson gson = new Gson();
twitterAuthToken = gson.fromJson(jsonAuth,
TwitterAuthToken.class);
} catch (IllegalStateException ex) {
}
}
return twitterAuthToken;
}

private ArrayList<TwitterTweet> convertJsonToTwitterTweet(String
twitterTweets) {
ArrayList<TwitterTweet> twitterTweetArrayList = null;
if (twitterTweets != null && twitterTweets.length() > 0) {
try {
Gson gson = new Gson();
twitterTweetArrayList = gson.fromJson(twitterTweets, new
TypeToken<ArrayList<TwitterTweet>>() {
}.getType());
} catch (IllegalStateException e) {
}
}
return twitterTweetArrayList;
}

private class TwitterAuthToken {
String token_type;
String access_token;
}

日志猫:

02-12 12:10:27.684: E/AndroidRuntime(30448): FATAL EXCEPTION: AsyncTask #1 02-12 12:10:27.684: E/AndroidRuntime(30448): java.lang.RuntimeException: An error occured while executing doInBackground() 02-12 12:10:27.684: E/AndroidRuntime(30448): at android.os.AsyncTask$3.done(AsyncTask.java:299) 02-12 12:10:27.684: E/AndroidRuntime(30448): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 02-12 12:10:27.684: E/AndroidRuntime(30448): at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 02-12 12:10:27.684: E/AndroidRuntime(30448): at java.util.concurrent.FutureTask.run(FutureTask.java:239) 02-12 12:10:27.684: E/AndroidRuntime(30448): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 02-12 12:10:27.684: E/AndroidRuntime(30448): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 02-12 12:10:27.684: E/AndroidRuntime(30448): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 02-12 12:10:27.684: E/AndroidRuntime(30448): at java.lang.Thread.run(Thread.java:864) 02-12 12:10:27.684: E/AndroidRuntime(30448): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 10 02-12 12:10:27.684: E/AndroidRuntime(30448): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:180) 02-12 12:10:27.684: E/AndroidRuntime(30448): at com.google.gson.Gson.fromJson(Gson.java:755) 02-12 12:10:27.684: E/AndroidRuntime(30448): at com.google.gson.Gson.fromJson(Gson.java:721) 02-12 12:10:27.684: E/AndroidRuntime(30448): at com.google.gson.Gson.fromJson(Gson.java:670) 02-12 12:10:27.684: E/AndroidRuntime(30448): at com.google.gson.Gson.fromJson(Gson.java:642) 02-12 12:10:27.684: E/AndroidRuntime(30448): at com.app.movie_name_rating.TwitterAPI.convertJsonToTwitterAuthToken(TwitterAPI.java:70) 02-12 12:10:27.684: E/AndroidRuntime(30448): at com.app.movie_name_rating.TwitterAPI.getTwitterAuthToken(TwitterAPI.java:62) 02-12 12:10:27.684: E/AndroidRuntime(30448): at com.app.movie_name_rating.TwitterAPI.getTwitterTweets(TwitterAPI.java:34) 02-12 12:10:27.684: E/AndroidRuntime(30448): at com.app.movie_name_rating.TwitterAsyncTask.doInBackground(TwitterAsyncTask.java:22) 02-12 12:10:27.684: E/AndroidRuntime(30448): at com.app.movie_name_rating.TwitterAsyncTask.doInBackground(TwitterAsyncTask.java:1) 02-12 12:10:27.684: E/AndroidRuntime(30448): at android.os.AsyncTask$2.call(AsyncTask.java:287) 02-12 12:10:27.684: E/AndroidRuntime(30448): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 02-12 12:10:27.684: E/AndroidRuntime(30448): ... 4 more 02-12 12:10:27.684: E/AndroidRuntime(30448): Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 10 02-12 12:10:27.684: E/AndroidRuntime(30448): at com.google.gson.stream.JsonReader.expect(JsonReader.java:339) 02-12 12:10:27.684: E/AndroidRuntime(30448): at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322) 02-12 12:10:27.684: E/AndroidRuntime(30448): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:168) 02-12 12:10:27.684: E/AndroidRuntime(30448): ... 15 more

最佳答案

如果有人仍在寻找解决方案,那就是:

1) 首先也是最重要的事情是将 API key 和 API key 替换为您自己生成的 key 。

2) 出现异常是因为 API key 和 API key 未替换,因此根据上面的 logcat 输出,它在第 70 行失败:--

    com.app.movie_name_rating.TwitterAPI.convertJsonToTwitterAuthToken(TwitterAPI.java:70) 02-12 12:10:27.684: E/AndroidRuntime(30448): at 
com.app.movie_name_rating.TwitterAPI.getTwitterAuthToken(TwitterAPI.java:62) 02-12 12:10:27.684: E/AndroidRuntime(30448): at

而不是返回具有以下格式的有效访问 token 的有效 JSON 字符串--

{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAANOmuQAAAAAAeIchMrbQdzP09sNyZYAtJQsLZ3k%3DhZuwbNpKTgHdvbhLS7ud3EdIUxz7iqZQyoAL4JhpBSf5tgEnTC"}

它返回一个字符串Forbidden,该字符串不是有效的 JSON 格式。

因此,理想情况下,您需要在异常中处理这种情况,而不是简单地吃掉以下代码 fragment 中的异常,这是一种不好的做法。

catch (IllegalStateException ex) {
}

关于java - 如何解决 com.google.gson.JsonException : java IllegalStateException :Expected BEGIN_OBJECT but was String at line 1 coulmn 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28471172/

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