我正在使用此代碼從JSON獲取數據。使用SimpleAdapter來自URL的圖像
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
。我不知道如何做到這一點。有什麼辦法嗎?
看看這個:http://cypressnorth.com/mobile-application-development/setting-android-google-volley-imageloader-networkimageview/或本http://blog.lemberg.co。英國/排雷部分3 - 圖像加載器,你必須加載圖像從該網址 –
試試這個鏈接 - http://stackoverflow.com/questions/16789676/caching-images-and-displaying/16978285#16978285 –