Android中Volley架包的使用
2015-11-04 08:56
399 查看
Volley架包中的StringRequest,JsonObjectRequest,ImageRequest可以分别进行网址的get,post解析,Json解析,图片解析。
首先还是把Volley架包复制到Android Studio中的libs目录下并刷新。
activity_main.xml
解析的数据的Log输出:
首先还是把Volley架包复制到Android Studio中的libs目录下并刷新。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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" android:padding="10dp"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="btnStringRequestGet" android:text="StringGet联网请求" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="btnStringRequestPost" android:text="StringPost联网请求" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="json" android:text="JsonRequest请求" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="image" android:text="ImageRequest请求" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>MainActivity:
package com.example.administrator.volleydemo; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.ImageView; import com.android.volley.AuthFailureError; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response.ErrorListener; import com.android.volley.Response.Listener; import com.android.volley.VolleyError; import com.android.volley.toolbox.ImageRequest; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONObject; import java.util.HashMap; import java.util.Map; /** * 1、创建RequestQueue,用于管理联网请求 * 2、创建一个联网请求:StringRequest,JsonObjectRequest, * 3、把创建的Request添加到RequestQueue中,Volley会自动联网请求,把数据返回到借口ImageRequest回调中 */ public class MainActivity extends AppCompatActivity { private String urlGet = "http://218.244.149.129:9010/api/companylist.php?industryid=99"; private String urlPost = "http://218.244.149.129:9010/api/companylist.php"; private String jsonUrl = "http://mobile.ximalaya.com/m/category_tag_menu"; private String imageUrl = "https://www.baidu.com/img/bd_logo1.png"; private RequestQueue queue; private StringRequest requestGet; private StringRequest requestPost; private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.imageView); //创建RequestQueue queue = Volley.newRequestQueue(this); } /** * StringRequest Get请求 * * @param view */ public void btnStringRequestGet(View view) { //Get请求 requestGet = new StringRequest(Request.Method.GET, urlGet, new Listener<String>() { @Override public void onResponse(String s) { Log.i("main", "get-->" + s); } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { } } ); queue.add(requestGet); } /** * StringRequest Post请求 * * @param view */ public void btnStringRequestPost(View view) { requestPost = new StringRequest(Request.Method.POST, urlPost, new Listener<String>() { @Override public void onResponse(String s) { Log.i("main", "post-->" + s); } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<>(); map.put("industryid", "99"); return map; } }; queue.add(requestPost); } /** * 进行JsonRequest请求 * * @param view */ public void json(View view) { JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(jsonUrl, null,//购物软件一般需要传递商品的Json对象 new Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { Log.i("main", "json--->" + jsonObject.toString()); } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { } }); queue.add(jsonObjectRequest); } /** * 进行ImageRequest请求 * * @param view */ public void image(View view) { ImageRequest imageRequest = new ImageRequest(imageUrl, new Listener<Bitmap>() { @Override public void onResponse(Bitmap bitmap) { if (bitmap != null) { iv.setImageBitmap(bitmap); } } }, 256, 128, Bitmap.Config.RGB_565, new ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { } }); queue.add(imageRequest); } }运行结果:
解析的数据的Log输出:
相关文章推荐
- Facebook Android客户端热更新
- android异常
- ”数独“android小游戏
- android81 多线程下载和断电续传
- Android笔记(五十一) 短信验证码集成——mob平台
- Android中实现Bitmap在自定义View中的放大与拖动
- Android中实现Bitmap在自定义View中的放大与拖动
- 关于android R.layout 中找不到已存在的布局文件问题的解决
- Android开发之ListView添加多种布局效果演示
- android MVP设计模式
- Android之Activity概述
- Android关于百分比布局-android.support.percent.PercentFram
- Genymotion启动失败解决方法
- Android 开发中,有哪些坑需要注意
- Android-常用第三方库proguard-rules配置
- Android  Windows悬浮窗
- cmd powerCmd adb logcat命令查看并过滤android输出log
- Android四种常用布局
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- #Retrofit 入门--Part 1