Volley学习
来源:互联网 发布:windows打补丁 编辑:程序博客网 时间:2024/05/18 15:07
布局文件代码
<?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:background="#fff" android:orientation="vertical"> <Button android:layout_width="match_parent" android:id="@+id/myvolley_stringrequest" android:layout_height="wrap_content" android:text="StringRequest" /> <Button android:id="@+id/myvolley_jsonarrayrequest" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="JsonArrayRequest" /> <Button android:id="@+id/myvolley_jsonobjectrequest" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="JsonObjectRequest " /> <Button android:id="@+id/myvolley_imagerequest" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ImageRequest " /> <Button android:id="@+id/myvolley_imageloader" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ImageLoader" /> <Button android:layout_width="match_parent" android:text="XmlRequest" android:id="@+id/myvolley_xmlrequest" android:layout_height="wrap_content" /> <ImageView android:id="@+id/myvolley_imageview" android:layout_width="match_parent" android:background="@mipmap/ic_launcher" android:layout_height="300dp" /> <com.android.volley.toolbox.NetworkImageView android:layout_width="match_parent" android:id="@+id/myvolley_networkimageview" android:layout_gravity="center_horizontal" android:layout_height="match_parent" /></LinearLayout>
java文件代码
其中ZJ()方法为总体的总结,没有任何的调用
package com.huida.demos.volley;import android.app.Fragment;import android.graphics.Bitmap;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.util.LruCache;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.ImageView;import com.android.volley.AuthFailureError;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.ImageRequest;import com.android.volley.toolbox.JsonArrayRequest;import com.android.volley.toolbox.JsonObjectRequest;import com.android.volley.toolbox.NetworkImageView;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;import com.huida.demos.R;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import java.io.IOException;import java.util.HashMap;import java.util.Map;/** * Created by lenovo on 2017/4/25. */public class MyVolleyRequest extends Fragment implements View.OnClickListener { private View inflate; private RequestQueue queue; private ImageView imageView; private NetworkImageView niv; public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { inflate = inflater.inflate(R.layout.myvolleystringrequest, container, false); initView(); initData(); initEvent(); return inflate; } public void ZJ(){ RequestQueue queue = Volley.newRequestQueue(getActivity()); StringRequest stringRequest = new StringRequest("url", new Response.Listener<String>() { @Override public void onResponse(String response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } });//返回一个字符串 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("url", new JSONObject(), new Response.Listener<JSONObject>() { //new JSONObject() 如果穿null为get方式上传,不为null为post方式跳转 @Override public void onResponse(JSONObject response) { //JSONObject类型 try { new JSONObject().put("key","value"); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { public void onErrorResponse(VolleyError error) { } });//jsonobjectRequest返回json Object类型数据 JsonArrayRequest jsonArrayRequest = new JsonArrayRequest("url", new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { //解析response为JSONArray类型 } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } });//jsonArrayRequest queue.add(stringRequest); queue.add(jsonObjectRequest); queue.add(jsonArrayRequest); } private void initData() { queue = Volley.newRequestQueue(getActivity()); } private void initEvent() { } private void initView() { Button StringRequest = (Button) inflate.findViewById(R.id.myvolley_stringrequest); StringRequest.setOnClickListener(this); Button ObjectRequest = (Button) inflate.findViewById(R.id.myvolley_jsonobjectrequest); ObjectRequest.setOnClickListener(this); Button ArrayRequest = (Button) inflate.findViewById(R.id.myvolley_jsonarrayrequest); ArrayRequest.setOnClickListener(this); Button ImageRequest = (Button) inflate.findViewById(R.id.myvolley_imagerequest); ImageRequest.setOnClickListener(this); Button ImageLoader = (Button) inflate.findViewById(R.id.myvolley_imageloader); ImageLoader.setOnClickListener(this); imageView = (ImageView) inflate.findViewById(R.id.myvolley_imageview); niv = (NetworkImageView) inflate.findViewById(R.id.myvolley_networkimageview); Button xml = (Button) inflate.findViewById(R.id.myvolley_xmlrequest); xml.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.myvolley_stringrequest: StringRequestEvent(); break; case R.id.myvolley_jsonobjectrequest: JSONObjectRequest(); break; case R.id.myvolley_jsonarrayrequest: JSONArrayRequest(); break; case R.id.myvolley_imagerequest: Log.d("volley","imagerequest"); ImageRequest(); break; case R.id.myvolley_imageloader: ImageLoader(); break; case R.id.myvolley_xmlrequest: MyVolleyXmlRequest xmlRequest = new MyVolleyXmlRequest( "http://flash.weather.com.cn/wmaps/xml/china.xml", 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("TAG", error.getMessage(), error); } }); queue.add(xmlRequest); break; default: } } private void ImageLoader() { ImageLoader loader = new ImageLoader(queue, new BitmapCache());// ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher);// loader.get("http://10.0.2.2:8080/ss/image/index/back1.jpg",listener); niv.setDefaultImageResId(R.mipmap.ic_launcher); niv.setErrorImageResId(R.mipmap.ic_launcher); niv.setImageUrl("http://img.my.csdn.net/uploads/201404/13/1397393290_5765.jpeg",loader); } class BitmapCache implements ImageLoader.ImageCache { private LruCache<String, Bitmap> mCache; public BitmapCache() { 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); } } private void ImageRequest() { ImageRequest request = new ImageRequest("http://10.0.2.2:8080/ss/image/index/back1.jpg", new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap response) { imageView.setImageBitmap(response); Log.d("volley", "success"); } }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("volley", error.getMessage(), error); } }); queue.add(request); } private void JSONArrayRequest() { new JsonArrayRequest("http://10.0.2.2:8080/ss/uploading.action", new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); } private void JSONObjectRequest() { JsonObjectRequest request = new JsonObjectRequest("http://10.0.2.2:8080/ss/uploading.action", null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }) { protected Map<String, String> getParams() throws AuthFailureError { return super.getParams(); } }; queue.add(request); } private void StringRequestEvent() { StringRequest request = new StringRequest("http://10.0.2.2:8080/ss/uploading.action", new Response.Listener<String>() { public void onResponse(String response) { //success Log.d("volley", "StringRequest onResponse Success" + response); } }, new Response.ErrorListener() { public void onErrorResponse(VolleyError error) { Log.d("volley", "StringRequest onErrorResponse " + error.getMessage(), error); } }){ protected Map<String, String> getParams() throws AuthFailureError {//用于Post请求传送参数 Map<String,String> map = new HashMap<String, String>(); return map; } }; queue.add(request); }}
0 0
- volley 学习
- volley学习
- volley 学习
- Volley学习
- Volley学习
- volley学习(1)—volley的特性
- Volley学习(一)Android Volley源码解析
- volley的学习
- Android Volley学习
- Volley 学习 2
- Volley学习资源
- volley开源库的学习
- Android组件Volley学习
- Volley学习之一AtomicInteger
- Volley学习笔记
- volley学习笔记
- 深入volley学习
- Volley学习小结
- 栈的使用-迷宫算法实现代码-摘自数据结构实现java版本(个人笔记整理)
- HTML常用标签
- 【源码剖析】MemoryPool —— 简单高效的内存池 allocator 实现
- Java异常类(Throwable)
- Oracle EBS 应用实例管理
- Volley学习
- Android插件化开发原理
- spring mvc 配合ajax进行表单提交(有文件上传)并且提醒用户提交成功
- 当你选择编程语言时你在选择什么
- run: read failed, socket might closed or timeout, read ret: -1
- Oracle Rac 部署详细过程
- 绑定本地Service和远程Service并调用其中方法
- PAT1010. 一元多项式求导 (25)
- 社会工程学攻击之看我如何用office文档欺骗用户