main.xml
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/btn_test_string" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="StringRequest" /> <Button android:id="@+id/btn_test_json" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="JsonRequest" /> <Button android:id="@+id/btn_test_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="ImageRequest" /> <Button android:id="@+id/btn_test_networkimageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="NewWorkImageView" /> <Button android:id="@+id/btn_test_imageloader" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="ImageLoader" /> <Button android:id="@+id/btn_test_myxml" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="MyXMLRequest" /> <Button android:id="@+id/btn_test_myjson" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="MyJsonRequest" /> <ImageView android:id="@+id/iv_test_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" /> <com.android.volley.toolbox.NetworkImageView android:id="@+id/network_image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> </LinearLayout>
MainActivity
package com.example.volleydemo; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONObject; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.ImageLoader.ImageListener; import com.android.volley.toolbox.ImageRequest; import com.android.volley.toolbox.JsonArrayRequest; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.JsonRequest; import com.android.volley.toolbox.NetworkImageView; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import com.example.volleydemo.bean.Weather; import com.example.volleydemo.bean.WeatherInfo; import com.example.volleydemo.volleyRequest.MyGsonRequest; import com.example.volleydemo.volleyRequest.MyXMLRequest; import android.os.Bundle; import android.support.v4.util.LruCache; import android.util.Log; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.Toast; /** * 本案例参考: http://www.apihome.cn/view-detail-70212.html http://www.apihome.cn/view-detail-70213.html * */ public class MainActivity extends Activity { public static final String LOG_TAG = "MainActivity"; private Button btnString, btnJson, btnImage, btnMyXml, btnMyJson, btnImagerLoader, btnNewWorkImageView; private ImageView imageView; private NetworkImageView networkImageView; private RequestQueue requestQueue; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initButton(); requestQueue = Volley.newRequestQueue(this); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); // if(requestQueue!=null){ // requestQueue.cancelAll(); // } } private void initButton() { btnString = (Button) this.findViewById(R.id.btn_test_string); btnJson = (Button) this.findViewById(R.id.btn_test_json); btnImage = (Button) this.findViewById(R.id.btn_test_image); btnMyXml = (Button) this.findViewById(R.id.btn_test_myxml); btnMyJson = (Button) this.findViewById(R.id.btn_test_myjson); btnImagerLoader = (Button) this.findViewById(R.id.btn_test_imageloader); btnNewWorkImageView = (Button) this.findViewById(R.id.btn_test_networkimageview); imageView = (ImageView) this.findViewById(R.id.iv_test_image); networkImageView = (NetworkImageView) this.findViewById(R.id.network_image_view); ButtonOnClickLisener btnListener = new ButtonOnClickLisener(); btnString.setOnClickListener(btnListener); btnJson.setOnClickListener(btnListener); btnImage.setOnClickListener(btnListener); btnMyXml.setOnClickListener(btnListener); btnMyJson.setOnClickListener(btnListener); btnImagerLoader.setOnClickListener(btnListener); btnNewWorkImageView.setOnClickListener(btnListener); } private class ButtonOnClickLisener implements View.OnClickListener { @Override public void onClick(View v) { String url; switch (v.getId()) { case R.id.btn_test_string: // 创建请求 url = "http://www.baidu.com"; StringRequest strReq = new StringRequest(url, new Response.Listener<String>() { @Override public void onResponse(String arg0) { Log.v(LOG_TAG, "StringRequest onResponse result->" + arg0); Toast.makeText(MainActivity.this, "onResponse返回的长度:" + arg0.length(), Toast.LENGTH_SHORT) .show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { Log.v(LOG_TAG, "onErrorResponse message->" + arg0.getMessage()); } }); // 添加到请求队列中 requestQueue.add(strReq); break; case R.id.btn_test_json: // url = "http://app.bang365.cn/home/api/"; JsonObjectRequest joReq = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject arg0) { Log.d("TAG", arg0.toString()); Toast.makeText(MainActivity.this, "json:" + arg0.toString(), Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { Log.v(LOG_TAG, "onErrorResponse message->" + arg0.getMessage()); } }); // 添加到请求队列中 requestQueue.add(joReq); break; case R.id.btn_test_image: url = "https://www.baidu.com/img/bdlogo.png"; /** * 第三第四个参数分别用于指定允许图片最大的宽度和高度,如果指定的网络图片的宽度或高度大于这里的最大值,则会对图片进行压缩, * 指定成0的话就表示不管图片有多大,都不会进行压缩。第五个参数用于指定图片的颜色属性,Bitmap. * Config下的几个常量都可以在这里使用,其中ARGB_8888可以展示最好的颜色属性,每个图片像素占据4个字节的大小, * 而RGB_565则表示每个图片像素占据2个字节大小。 */ ImageRequest imReq = new ImageRequest(url, new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap arg0) { imageView.setImageBitmap(arg0); } }, 0, 0, Config.RGB_565, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { Log.v(LOG_TAG, "onErrorResponse message->" + arg0.getMessage()); imageView.setImageResource(R.drawable.ic_launcher); } }); requestQueue.add(imReq); break; case R.id.btn_test_imageloader: url = "https://www.baidu.com/img/bdlogo.png"; ImageLoader imageLoader = new ImageLoader(requestQueue, new MyImageCache()); ImageListener imageListener = imageLoader.getImageListener(imageView, R.drawable.default_icon, R.drawable.error_icon); // imageLoader.get(url, imageListener); imageLoader.get(url, imageListener, 200, 200); break; case R.id.btn_test_networkimageview: url = "http://pic13.nipic.com/20110309/6094063_105618524133_2.jpg"; ImageLoader networkimageLoader = new ImageLoader(requestQueue, new MyImageCache()); networkImageView.setDefaultImageResId(R.drawable.default_icon); networkImageView.setErrorImageResId(R.drawable.error_icon); networkImageView.setImageUrl(url, networkimageLoader); break; case R.id.btn_test_myxml: // 返回天气预报 url = "http://flash.weather.com.cn/wmaps/xml/china.xml"; MyXMLRequest xmlRequest = new MyXMLRequest(url, new Response.Listener<XmlPullParser>() { @Override public void onResponse(XmlPullParser response) { try { int eventType = response.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: String nodeName = response.getName(); if ("city".equals(nodeName)) { String pName = response.getAttributeValue(0); Log.d("TAG", "pName is " + pName); } break; } eventType = response.next(); } } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e(LOG_TAG, "onErrorResponse message:" + error.getMessage(), error); } }); requestQueue.add(xmlRequest); break; case R.id.btn_test_myjson: url = "http://www.weather.com.cn/data/sk/101010100.html"; MyGsonRequest<Weather> mjReq = new MyGsonRequest<Weather>(url, Weather.class, new Response.Listener<Weather>() { @Override public void onResponse(Weather weather) { WeatherInfo weatherInfo = weather.getWeatherinfo(); Log.v("TAG", "city is " + weatherInfo.getCity()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e(LOG_TAG, "onErrorResponse message:" + error.getMessage(), error); } }); requestQueue.add(mjReq); break; } } } private class MyImageCache implements ImageLoader.ImageCache { private LruCache<String, Bitmap> mCache; public MyImageCache() { // 缓存图片的大小设置为10M int maxSize = 10 * 1024 * 1024; mCache = new LruCache<String, Bitmap>(maxSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getRowBytes() * bitmap.getHeight(); } }; } @Override public Bitmap getBitmap(String url) { return mCache.get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { mCache.put(url, bitmap); } } }
自定义MyJsonRequest
package com.example.volleydemo.volleyRequest; import java.io.StringReader; import java.io.UnsupportedEncodingException; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import com.android.volley.NetworkResponse; import com.android.volley.ParseError; import com.android.volley.Request; import com.android.volley.Request.Method; import com.android.volley.Response; import com.android.volley.Response.ErrorListener; import com.android.volley.Response.Listener; import com.android.volley.toolbox.HttpHeaderParser; import com.google.gson.Gson; public class MyGsonRequest<T> extends Request<T> { private final Listener<T> mListener; private Gson mGson; private Class<T> mClass; public MyGsonRequest(int method, String url, Class<T> clazz, Listener<T> listener, ErrorListener errorListener) { super(method, url, errorListener); mGson = new Gson(); mClass = clazz; mListener = listener; } public MyGsonRequest(String url, Class<T> clazz, Listener<T> listener, ErrorListener errorListener) { this(Method.GET, url, clazz, listener, errorListener); } @Override protected Response<T> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); return Response.success(mGson.fromJson(jsonString, mClass), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } } @Override protected void deliverResponse(T response) { mListener.onResponse(response); } }
自定义MyXmlRequest
package com.example.volleydemo.volleyRequest; import java.io.StringReader; import java.io.UnsupportedEncodingException; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import com.android.volley.NetworkResponse; import com.android.volley.ParseError; import com.android.volley.Request; import com.android.volley.Request.Method; import com.android.volley.Response; import com.android.volley.Response.ErrorListener; import com.android.volley.Response.Listener; import com.android.volley.toolbox.HttpHeaderParser; /** * 参考:http://www.apihome.cn/view-detail-70213.html * */ public class MyXMLRequest extends Request<XmlPullParser> { private final Listener<XmlPullParser> mListener; public MyXMLRequest(int method, String url, Listener<XmlPullParser> listener, ErrorListener errorListener) { super(method, url, errorListener); mListener = listener; } public MyXMLRequest(String url, Listener<XmlPullParser> listener, ErrorListener errorListener) { this(Method.GET, url, listener, errorListener); } @Override protected Response<XmlPullParser> parseNetworkResponse(NetworkResponse response) { try { String xmlString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser = factory.newPullParser(); xmlPullParser.setInput(new StringReader(xmlString)); return Response.success(xmlPullParser, HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (XmlPullParserException e) { return Response.error(new ParseError(e)); } } @Override protected void deliverResponse(XmlPullParser response) { mListener.onResponse(response); } }
两个bean
package com.example.volleydemo.bean; public class Weather { private WeatherInfo weatherinfo; public WeatherInfo getWeatherinfo() { return weatherinfo; } public void setWeatherinfo(WeatherInfo weatherinfo) { this.weatherinfo = weatherinfo; } }
package com.example.volleydemo.bean; public class WeatherInfo { private String city; private String temp; private String time; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getTemp() { return temp; } public void setTemp(String temp) { this.temp = temp; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } }
另:
1、在androidmainfest.xml中添加网络权限;
2、导入volley、json的包。
本文主要参考:
本案例参考:
http://www.apihome.cn/view-detail-70212.html
http://www.apihome.cn/view-detail-70213.html
时间: 2024-10-11 05:12:54