如何从 jsonarray 解析图像并使用 Picasso 显示
How to parse image from jsonarray and display using Picasso
我有这个JSON data
我必须解析具有 3 个图像链接的键 "im:image" 的 JSONArray
,并使用 Picasso 库显示它。
有什么想法吗?提前致谢。
尝试以下方法
try {
JSONObject jsonFeed = new JSONObject(your_original_json).getJSONObject("feed");
JSONArray entries = jsonFeed.getJSONArray("entry");
int count = entries.length();
for (int i = 0; i < count; i++) {
JSONObject imageJson = entries.getJSONObject(i).getJSONObject("im:image");
// in case you want to get image with height 53
String imageUrl = imageJson.getJSONObject("0").getString("label");
// String imageUrl = imageJson.getJSONObject("1").getString("label"); height 75
// String imageUrl = imageJson.getJSONObject("2").getString("label"); height 100
Picasso.with(context)
.load(imageUrl)
.into(imageView);
}
} catch (JSONException e) {
e.printStackTrace();
}
您必须使用 HttpClient 从您的 URL 获取 Json。下面的代码。
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://itunes.apple.com/jo/rss/topfreeapplications/limit=50/json");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream weps = entity.getContent();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(weps, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
weps.close();
result = sb.toString();//result string defined above will store your json.
} catch (Exception e) {
Log.e("log_tag", "Error Coverting result" + e.toString());
}
} catch (Exception e) {
Log.e("log_tag", "Error Http connection" + e.toString());
}
try {
JSONArray entries = new JSONArray(result);
int count = entries.length();
for (int i = 0; i < count; i++) {
JSONObject imageJson= entries.getJSONObject(i).getJSONObject("im:image");
// in case you want to get image with height 53
String imageUrl = imageJson.getJSONObject("0").getString("label");
// String imageUrl = imageJson.getJSONObject("1").getString("label"); height 75
// String imageUrl = imageJson.getJSONObject("2").getString("label"); height 100
Picasso.with(context)//context variable of the class
.load(imageUrl)// url from Json
.into(imageView); //your image view object
}
} catch (Exception e) {
Log.e("log_tag", "Error Parsing Dataaaa" + e.toString());
}
我有这个JSON data
我必须解析具有 3 个图像链接的键 "im:image" 的 JSONArray
,并使用 Picasso 库显示它。
有什么想法吗?提前致谢。
尝试以下方法
try {
JSONObject jsonFeed = new JSONObject(your_original_json).getJSONObject("feed");
JSONArray entries = jsonFeed.getJSONArray("entry");
int count = entries.length();
for (int i = 0; i < count; i++) {
JSONObject imageJson = entries.getJSONObject(i).getJSONObject("im:image");
// in case you want to get image with height 53
String imageUrl = imageJson.getJSONObject("0").getString("label");
// String imageUrl = imageJson.getJSONObject("1").getString("label"); height 75
// String imageUrl = imageJson.getJSONObject("2").getString("label"); height 100
Picasso.with(context)
.load(imageUrl)
.into(imageView);
}
} catch (JSONException e) {
e.printStackTrace();
}
您必须使用 HttpClient 从您的 URL 获取 Json。下面的代码。
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://itunes.apple.com/jo/rss/topfreeapplications/limit=50/json");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream weps = entity.getContent();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(weps, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
weps.close();
result = sb.toString();//result string defined above will store your json.
} catch (Exception e) {
Log.e("log_tag", "Error Coverting result" + e.toString());
}
} catch (Exception e) {
Log.e("log_tag", "Error Http connection" + e.toString());
}
try {
JSONArray entries = new JSONArray(result);
int count = entries.length();
for (int i = 0; i < count; i++) {
JSONObject imageJson= entries.getJSONObject(i).getJSONObject("im:image");
// in case you want to get image with height 53
String imageUrl = imageJson.getJSONObject("0").getString("label");
// String imageUrl = imageJson.getJSONObject("1").getString("label"); height 75
// String imageUrl = imageJson.getJSONObject("2").getString("label"); height 100
Picasso.with(context)//context variable of the class
.load(imageUrl)// url from Json
.into(imageView); //your image view object
}
} catch (Exception e) {
Log.e("log_tag", "Error Parsing Dataaaa" + e.toString());
}