我正在使用JSON解析器来解析JSON响应,我可以在ListView中获取项目名称,但无法获取与其对应的图像。JSON响应中存在图像url 的位置。如何解析响应以获取与项目对应的图像。
我的代码..
jsonParser.java
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();
}
}