我正在使用 JSON 解析器来解析 JSON 响应,我可以在 ListView 中获取项目名称,但我无法获得与之对应的图像。 JSON 响应中存在图像 url的的位置。如何解析响应以获取与项目对应的图像。
我的代码..
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
然后在主课堂 ..............................
JSONParser jParser1 = new JSONParser();
json1 = jParser1.getJSONFromUrl(url0);
try {
Log.d("Parsing JSON Data", "Before json1 try2");
activities = json1.getJSONArray(TAG_ACTIVITIES);
Log.d("Parsing JSON Data", "after activiti");
for (int i = 0; i < activities.length(); i++) {
JSONObject c = activities.getJSONObject(i);
Log.d("Parsing JSON Data", "inside for loop");
String time = c.getString("time");
user_name = c.getString("user_name");
// Image_url=c.getString("thumb");
// BitmapFactory.Options bmOptions;
// bmOptions = new BitmapFactory.Options();
// bmOptions.inSampleSize = 1;
// Bitmap bm = LoadImage(Image_url, bmOptions);
String utype = c.getString("type");
HashMap<String, Object> temp11 = new HashMap<String, Object>();
Log.d("Parsing JSON Data", "inside if");
if (utype.contains("user")) {
temp11.put(TAG_USER_NAME, user_name + " " + join);
} else if (utype.contains("checkin")) {
subscriber_name = c.getString("subscriber_name");
temp11.put(TAG_USER_NAME, user_name + " " + checkin1 + " "
+ subscriber_name);
} else {
subscriber_name = c.getString("subscriber_name");
temp11.put(TAG_USER_NAME, user_name + " " + favorite1 + " "
+ subscriber_name);
// temp11.put("subscriber_name", subscriber_name);
}
temp11.put(TAG_TIME, time);
temp11.put("image1", bm);
list13.add(temp11);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以使用Android-Query(AQuery)或Smart image view。这有助于您轻松地从json加载图片
答案 2 :(得分:0)