0
我将json对象转换为java对象,但我不断收到错误 com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:未终止的对象在第1行第20列路径$ [0 ] .urlToImageGSON格式错误的url异常
这里是我的代码
public void onResponse(JSONObject response) {
JSONArray recArticles = new JSONArray();
try {
recArticles = response.getJSONArray("articles");
for (int i=0;i<recArticles.length();i++) {
JSONObject jsonObject = recArticles.getJSONObject(i);;
String author = jsonObject.getString("author");
String title = jsonObject.getString("title");
String description = jsonObject.getString("description");
String url = jsonObject.getString("url");
String urlToImage = jsonObject.getString("urlToImage");
HashMap<String, String> singleArticle = new HashMap<String, String>();
singleArticle.put("author", author);
singleArticle.put("title", title);
singleArticle.put("description", description);
singleArticle.put("url", url);
singleArticle.put("urlToImage", urlToImage);
mArticleList.add(String.valueOf(singleArticle));
}
} catch (JSONException e) {
e.printStackTrace();
}
Gson gson = new Gson();
Type articleListType = new TypeToken<Collection<RecArticle>>() {}.getType();
Log.d(TAG, "onResponse: " + mArticleList);
Log.d(TAG, "onResponse: " + String.valueOf(mArticleList));
mArticles = gson.fromJson(String.valueOf(mArticleList), articleListType);
mArticleAdapter = new RecommenderAdapter(getContext(), mArticles);
mRecyclerView.setAdapter(mArticleAdapter);
mAviLib.hide();
mRefreshLayout.setRefreshing(false);
}
把你Ĵ SON请,错误表示JSON格式不正确。 –
这里是链接https://newsapi.org/v1/articles?source=google-news&sortBy=top&apiKey=2743bc85885242bbade88d6c0a25c511 –
Gson.fromJson需要一个json字符串。我猜mArticleList不是JSON字符串。 –