您的位置:首页 > 移动开发 > Android开发

Android中Volley架包的使用

2015-11-04 08:56 399 查看
Volley架包中的StringRequest,JsonObjectRequest,ImageRequest可以分别进行网址的get,post解析,Json解析,图片解析。

首先还是把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输出:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: