我正在使用此代码从JSON获取数据。
public class ShowContacts extends ListActivity{
private static String url = "http://192.168.0.103/contacts.json";
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_NAME = "name";
private static final String TAG_IMAGE = "image";
JSONArray contacts = null;
int index = 0;
ArrayList<HashMap<String, String>> contactList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showjson);
contactList = new ArrayList<HashMap<String, String>>();
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler sh = new ServiceHandler();
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String name = c.getString(TAG_NAME);
String image = c.getString(TAG_IMAGE);
HashMap<String, String> contact = new HashMap<String, String>();
contact.put(TAG_NAME, name);
contact.put(TAG_IMAGE, image);
contactList.add(contact);
}
} catch (JSONException e){e.printStackTrace();}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
String[] from = { TAG_NAME, TAG_IMAGE };
int[] to = { R.id.name, R.id.image };
ListAdapter adapter = new SimpleAdapter(
ShowContacts.this, contactList, R.layout.list_item, from , to );
setListAdapter(adapter);
}
}
}
我想从JSON获取名称和图片。我得到了名字,它显示在列表中,但我无法获得图像。这是我的JSON:
{
"contacts": [
{
"name": "John Smith",
"image": "http://192.168.0.103/image1.png"
},
{
"name": "John Wayne",
"image": "http://192.168.0.103/image2.png"
}
]
}
我认为它不起作用,因为图像处于联机状态,并且它尝试将其添加为Drawable
。我不知道该怎么做。有什么办法吗?
答案 0 :(得分:0)
您应该使用LazyLoading下载图像并将其加载到ImageView
。您只需简单地解析并将网址设置为ImageView
即可直接加载图片。
答案 1 :(得分:0)
答案 2 :(得分:0)
公共类ItemImagesAdapter扩展了BaseAdapter {
private Context context;
private ArrayList<String> contactList;
private ImageLoader iml;
public ItemImagesAdapter(Context ctx, ArrayList<String> image_paths) {
context = ctx;
contactList = image_paths;
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(activity).build();
ImageLoader.getInstance().init(config);
iml = ImageLoader.getInstance();
}
@Override
public int getCount() {
return contactList.size();
}
@Override
public Object getItem(int position) {
return contactList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_image, parent, false);
}
String imagepath = contactList.get(position).getImagePaths();
ImageView image = (ImageView) convertView.findViewById(R.id.image1);
TextView tv = (TextView) convertView.findViewById(R.id.text1);
tv.setText(contactlist.get(position).getText);
iml.displayImage(imagepath, image);
return convertView;
}
}
在onPostExecute()
中 protected void onPostExecute(Void result) {
super.onPostExecute(result);
ItemImagesAdapter adapter = new ItemImagesAdapter(getAppicationContext(), contactlist);
listViewobj.setAdapter(adapter);
}
从你的主动活动中调用它。我希望你知道如何设置适配器.. 希望这有助于你
答案 3 :(得分:0)
从服务器下载图像的最佳方法之一是Android Query。您可以下载最多5k图像,没有任何内存错误。
从Here下载 Android查询jar并将其放入项目的libs文件夹中。
AQuery aq = new AQuery(_scontext); // Android query object
ImageView _imageObject= (ImageView) itemView.findViewById(R.id.product_image); // your imageview onbject
//并使用下面的代码,您可以在imageview中设置图像。
aq.id(_imageObject).image("image url as string");