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輸出: