public class MainActivity extends AppCompatActivity {
private String url;
private Context context;
private ImageView mImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
initView();
/**
* Volley三部曲
* 1.创建请求对象
* 2.创建请求队列
* 3.发起请求
*/
url = "http://10.0.3.2:8080/json/student.json";
context = this;
}
public void stringRequest(View v) {
//1.创建请求对象
//第2个参数:成功回调接口,第3个参数:失败回调接口
StringRequest request = new StringRequest(url, new Response.Listener<String>() {
//成功调用该方法
@Override
public void onResponse(String s) {
try {
showToast(s);
showLog(s);
JSONObject json = new JSONObject(s);
String name = json.getString("name");//正式开发不建议使用,当key不存在,空指针
String name1 = json.optString("name");//正式开发建议使用,当key不存在,返回默认值
int age = json.optInt("age");
showLog("age=" + age + ",name=" + name1);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
//错误时调用该方法
@Override
public void onErrorResponse(VolleyError volleyError) {
showLog(volleyError.getMessage());
}
});
//2.创建请求队列
RequestQueue queue = Volley.newRequestQueue(context);
//3.发起请求
queue.add(request);
}
//返回json对象
public void jsonObjectRequest(View v) {
//1.
JSONObject jsonObject = null;//调用接口提交的参数,正式开发不使用这种方式提交参数,重写一个方法getPostParams
// jsonObject.put("password", "123456");
JsonObjectRequest request = new JsonObjectRequest(url, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
showLog(jsonObject.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
showLog(volleyError.getMessage());
}
}) {
@Override
protected Map<String, String> getPostParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("username", "heima26");
params.put("password", "123456");
// return super.getPostParams();
return params;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
// return super.getParams();
HashMap<String, String> params = new HashMap<String, String>();
params.put("username", "heima26");
params.put("password", "123456");
// return super.getPostParams();
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return super.getHeaders();
}
};
//2.
RequestQueue queue = Volley.newRequestQueue(context);
//3.
queue.add(request);
}
public void jsonArrayRequest(final View v) {
final String arrayUrl = "http://10.0.3.2:8080/json/students.json";
JsonArrayRequest request = new JsonArrayRequest(arrayUrl, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray jsonArray) {
showLog(jsonArray.toString());
//解析json数组
for (int i = 0; i < jsonArray.length(); i++) {
//获取每一个数组元素
JSONObject json = jsonArray.optJSONObject(i);
String name = json.optString("name");
int age = json.optInt("age");
showLog("i=" + i + ",name=" + name + ",age=" + age);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
showLog("" + volleyError.getMessage());
}
});
RequestQueue queue = Volley.newRequestQueue(context);
queue.add(request);
}
//返回bitmap对象
public void imageRequest(View v){
String imgUrl = "http://10.0.3.2:8080/imgs/sunny.jpg";
int maxWidth = 120;//0表示默认大小
int maxHeight = 120;//0表示默认大小,大于0对图片大图压缩
Bitmap.Config decodeConfig = Bitmap.Config.RGB_565;// 一个像素点占16位,2byte
// Bitmap.Config decodeConfig = Bitmap.Config.ALPHA_8;//一个像素点占8位,1byte
// Bitmap.Config decodeConfig = Bitmap.Config.ARGB_4444;//一个像素点占16位,2byte
// Bitmap.Config decodeConfig = Bitmap.Config.ARGB_8888;//一个像素点占32位,4byte
/**
* 练习:有一张图片 480 * 800,请计算以下不同图片格式占用大小:
* RGB_565 :480 * 800 * 2 byte = 除以1024=kb
*/
//1.创建请求对象
ImageRequest request = new ImageRequest(imgUrl, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
mImage.setImageBitmap(bitmap);
}
}, maxWidth, maxHeight, decodeConfig, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
showLog(volleyError.getMessage());
}
});
//2.创建请求队列
RequestQueue queue = Volley.newRequestQueue(context);
//3.发起网络请求
queue.add(request);
}
private void showLog(String msg) {
Log.e("result", "" + msg);
}
private void showToast(String msg) {
Toast.makeText(MainActivity.this, "" + msg, Toast.LENGTH_SHORT).show();
}
private void initView() {
mImage = (ImageView) findViewById(R.id.image);
}
}
Volley的四大请求
最新推荐文章于 2024-04-03 12:01:53 发布