ListView异步加载网络信息包括图片功能

本文介绍如何在Android应用中实现ListView异步加载网络信息,并展示实体信息及图片,通过发送HTTP请求获取数据,解析JSON,构建自定义适配器,最后展示在界面上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ListView异步加载网络信息包括图片功能实现思路

1.android端向服务器发送请求,服务器返回集合类型json字符串

2. android端解析json,显示实体信息

3. 显示图片时再次向服务器请求获取图片对象显示。

实例如下

android端代码 :

public class JsonActivity extends Activity {

	Button  button=null;
	final static int MSGWHAT=1 ;
	Handler handler=null;
	private ListView listView=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_json);
		//获取listView对象
		listView=(ListView)findViewById(R.id.listView1);
		new SendThread().start();

		//将服务器返回数据使用Toast显示
		handler=new Handler(){

			@Override
			public void handleMessage(Message msg) {
				if(msg.what==MSGWHAT){	
					UserAdapter userAdapter=(UserAdapter)msg.obj;
					listView.setAdapter(userAdapter);	
				}
			} 	
		};
	}
	
	/**
	 * 定义线程类调用web服务方法
	 * @author Administrator
	 */
	class SendThread extends Thread{
		@Override
		public void run() {
			String value=sendGet();
			ArrayList<HashMap<String, Object>> list=new ArrayList<HashMap<String,Object>>();
			//将json文本转换成java对象
			List<User> jsonArray=JSON.parseArray(value,User.class);
		 	for (Iterator iterator = jsonArray.iterator(); iterator.hasNext();) {
				User user = (User) iterator.next();
				HashMap<String, Object> hashMap=new HashMap<String, Object>();
				hashMap.put("name", user.getName());
				hashMap.put("id", user.getUid());
				hashMap.put("imagePath", user.getImagePath());
				list.add(hashMap);
				//构建适配器
				UserAdapter userAdapter=new UserAdapter(JsonActivity.this, list);
				
				Message message=handler.obtainMessage();
				message.what=MSGWHAT;
				message.obj=userAdapter;
				handler.sendMessage(message);
		 	}
	

		}

	}
	
	//使用 httpClient访问web 服务
	public String sendGet(){

		//定义访问web服务的url,IP根据情况可更改
		String url="http://192.168.43.11:8080/MyTest/JsonServlet";
		//使用get提交方式
		HttpGet httpGet=new HttpGet(url);
		//创建httpclient对象
		HttpClient httpClient=new DefaultHttpClient();
		
		HttpResponse httpResponse=null;
		String text=null;
		try {
			//以get方式向服务器发送请求
			httpResponse=httpClient.execute(httpGet);
			if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){//判断http响应是等于200
				//获取服务器返回信息
				 text=EntityUtils.toString(httpResponse.getEntity());
				 System.out.println("text:"+text);
			}
			return text;
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{

		}
		return null;	
	}
	
}
自定义适配器

public class UserAdapter extends BaseAdapter {
	private ArrayList<HashMap<String,Object>> data;
	private LayoutInflater layoutInflater;
	private Context context;
	public UserAdapter(Context context,ArrayList<HashMap<String, Object>> data){
		this.context=context;
		this.data=data;
		this.layoutInflater=LayoutInflater.from(context);
	}

	/**
	 * listview中显示项的总和
	 */
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	/**
	 * position:表示
	 * 
	 * 
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		 ZunJian zujian=null;
		if(convertView==null){
			 zujian=new ZunJian();
			//得到列表样式的View对象
			 convertView=layoutInflater.inflate(R.layout.activity_json_item, null);
			//得到item项每个组件
			zujian.usernameTextView=(TextView)convertView.findViewById(R.id.usernameTextView);
			zujian.useridTextView=(TextView)convertView.findViewById(R.id.useridTextView);
			zujian.imageView=(ImageView)convertView.findViewById(R.id.imgpath);
			
			convertView.setTag(zujian);
			
		}else{
			 zujian=(ZunJian)convertView.getTag();
			
		}
		System.out.println("MyAdapter==="+zujian);
		//把data中的值显示在每个控件中
		zujian.usernameTextView.setText(data.get(position).get("name").toString());
		zujian.useridTextView.setText(data.get(position).get("id").toString());
		loadImage(zujian.imageView,data.get(position).get("imagePath").toString());
		
		return convertView;
	}
	
	class ZunJian{
		TextView usernameTextView;
		TextView useridTextView;
		ImageView imageView;
		
		
	}
	Handler handler=null;
	Thread thread=null;
	
	
	public void loadImage(final ImageView imageView,final String imagePath){
		 handler=new  Handler(){
			@Override
			public void handleMessage(Message msg) {
				Bitmap bitmap=(Bitmap)msg.obj;
				imageView.setImageBitmap(bitmap);
			}
		};
		
		
		new Thread(){
			 
			@Override
			public void run() {
				//定义访问web服务的url,IP根据情况可更改
				String url="http://192.168.43.11:8080/MyTest/"+imagePath;
				//使用get提交方式
				HttpGet httpGet=new HttpGet(url);
				//创建httpclient对象
				HttpClient httpClient=new DefaultHttpClient();
				HttpResponse httpResponse=null;
				String text=null;
				try {
					//以get方式向服务器发送请求
					httpResponse=httpClient.execute(httpGet);
					if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){//判断http响应是等于200
						//获取服务器返回信息
				
						byte data[]=EntityUtils.toByteArray(httpResponse.getEntity());
						Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
						Message message=handler.obtainMessage();
						message.what=1;
						message.obj=bitmap;
						handler.sendMessage(message);
					}
					
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{

				}
				
			}
			
			
		}.start();
		
		
	}
	
}
JsonActivity 的配置文件<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>


</LinearLayout>

UserAdapter自定适配器文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
    <ImageView
        android:id="@+id/imgpath"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    
    <LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >




    
    <TextView
        android:id="@+id/usernameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <TextView
        android:id="@+id/useridTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    </LinearLayout>


</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值