HttpURLConnection,HttpClient,Volley(5)

Q:如果做到比如5个城市发到队列里去请求,然后返回的数据异步更新ListView?
notifyDataSetChanged? 拿到具体的Item的View去更新?
notifyDataSetChanged等于是一个个刷新所有的View。算了,为了方便起见,就等所有的城市都更新完数据后,调用一次notifyDataSetChanged来更新所有的列表。

先上效果动画:

这里写图片描述

代码:

manifest中不要忘记加网络权限 <uses-permission android:name="android.permission.INTERNET"/>

1

主界面Layout <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="50dp" > </ListView> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="Refresh" /> </RelativeLayout> ListView Item的Layout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20sp" android:layout_weight="1" android:text="城市" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/Weather" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginRight="20sp" android:text="Weather" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/Low_Temp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20sp" android:text="Low_Temp" /> <TextView android:id="@+id/to" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="--" /> <TextView android:id="@+id/Top_Temp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Low_Temp" /> <TextView android:id="@+id/Time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="1" android:gravity="right" android:paddingRight="20sp" android:text="Time" /> </LinearLayout> </LinearLayout> 自定义的BaseAdapter package com.example.volleyweather; import java.util.List; import java.util.Map; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; // public class weatherAdapter extends BaseAdapter { private List<Map<String, String>> weatherInfo; private LayoutInflater mInflater; public weatherAdapter(Context context,List<Map<String, String>> weatherInfo) { super(); this.mInflater = LayoutInflater.from(context); this.weatherInfo = weatherInfo; } @Override public int getCount() { // TODO Auto-generated method stub return weatherInfo.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder=new ViewHolder(); convertView = mInflater.inflate(R.layout.weather_info, null); holder.city = (TextView)convertView.findViewById(R.id.city); holder.weather= (TextView)convertView.findViewById(R.id.Weather); holder.lowTemp= (TextView)convertView.findViewById(R.id.Low_Temp); holder.topTemp= (TextView)convertView.findViewById(R.id.Top_Temp); holder.time= (TextView)convertView.findViewById(R.id.Time); convertView.setTag(holder); }else { holder = (ViewHolder)convertView.getTag(); } holder.city.setText(weatherInfo.get(position).get("city")); holder.weather.setText(weatherInfo.get(position).get("weather")); holder.lowTemp.setText(weatherInfo.get(position).get("temp1")); holder.topTemp.setText(weatherInfo.get(position).get("temp2")); holder.time.setText(weatherInfo.get(position).get("ptime")); return convertView; } final class ViewHolder{ public TextView city; public TextView weather; public TextView lowTemp; public TextView topTemp; public TextView time; } } MainActivity package com.example.volleyweather; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import org.json.JSONException; import org.json.JSONObject; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.Volley; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { String Tag="Weather"; ListView weatherList; Button refreshButton; List<Map<String, String>> weatherInfo=new ArrayList<Map<String, String>>(); int[] cityId = { 101010100, 101030100, 101040100, 101050101, 101060801 }; private RequestQueue mQueue; private weatherAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); weatherList = (ListView) findViewById(R.id.listView1); refreshButton = (Button) findViewById(R.id.button1); mQueue=Volley.newRequestQueue(this); myAdapter=new weatherAdapter(this,weatherInfo); weatherList.setAdapter(myAdapter); refreshButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { fetchWeather(weatherInfo); } }); } void fetchWeather(final List weatherInfo){ final AtomicInteger count=new AtomicInteger(0); //Request weather with Volly,save the result into weatherInfo list weatherInfo.clear(); for(int i=0;i<cityId.length;i++){ //make a jsonObjectRequest JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://www.weather.com.cn/data/cityinfo/"+cityId[i]+".html", null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject arg0) { Log.e(Tag,"weather got!!"); //iterator of the String names in this object. Iterator<String> it=arg0.keys(); while(it.hasNext()){ String key=it.next();//这个key对应weatherinfo JSONObject weatherinfoObj=null; try { weatherinfoObj=arg0.getJSONObject(key);//对应weatherinfo的整个结构 } catch (JSONException e) { e.printStackTrace(); } if(weatherinfoObj!=null){ Iterator<String> weatherItems = weatherinfoObj.keys();//分别是city,cityid等。 Map<String, String> map = new HashMap<String, String>(); while(weatherItems.hasNext()){ String weatherItem=weatherItems.next(); String weatherContent; try { weatherContent=weatherinfoObj.getString(weatherItem); map.put(weatherItem, weatherContent); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } weatherInfo.add(map); } } count.addAndGet(1); if(count.get()==cityId.length) { //如果返回的response个数达到了请求的个数,更新listView //Refresh the ListView myAdapter.notifyDataSetChanged(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { } }); mQueue.add(jsonObjectRequest); } }

不过这个网站有的时候刷新次数多了就不返回响应了,可能是网站加入了保护机制。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/bbb2a0b66b66e00712b3a71d4a856c84.html