• Android学习笔记--Volley加载框架


    加载图片的两种方法

     1  //Volley加载图片  ImageLoader使用法
     2 ImageLoader imageLoader = new ImageLoader(requestQueue,new ImageLoood());
     3         ImageLoader.ImageListener imageListener = imageLoader.getImageListener(imageView,R.mipmap.ic_launcher,R.mipmap.ic_launcher);
     4         imageLoader.get(strUrl,imageListener );
     5 
     6                 //ImageRequest加载方法
     7 
     8         ImageRequest request = new ImageRequest(strUrl, new Response.Listener<Bitmap>() {
     9             @Override
    10             public void onResponse(Bitmap response) {
    11                 imageView.setImageBitmap(response);
    12             }
    13         }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
    14             @Override
    15             public void onErrorResponse(VolleyError error) {
    16                 imageView.setImageResource(R.mipmap.ic_launcher);
    17             }
    18         });
    19         requestQueue.add(request);

    网络请求的

      1 package com.wuxianedu.avollys;
      2 
      3 import android.app.DownloadManager;
      4 import android.icu.util.Measure;
      5 import android.support.v7.app.AppCompatActivity;
      6 import android.os.Bundle;
      7 import android.view.View;
      8 import android.widget.TextView;
      9 
     10 import com.android.volley.AuthFailureError;
     11 import com.android.volley.NetworkResponse;
     12 import com.android.volley.Request;
     13 import com.android.volley.RequestQueue;
     14 import com.android.volley.Response;
     15 import com.android.volley.VolleyError;
     16 import com.android.volley.toolbox.HttpHeaderParser;
     17 import com.android.volley.toolbox.JsonArrayRequest;
     18 import com.android.volley.toolbox.JsonObjectRequest;
     19 import com.android.volley.toolbox.StringRequest;
     20 import com.android.volley.toolbox.Volley;
     21 
     22 import org.json.JSONArray;
     23 
     24 import java.io.UnsupportedEncodingException;
     25 import java.util.HashMap;
     26 import java.util.Map;
     27 
     28 public class MainActivity extends AppCompatActivity implements View.OnClickListener{
     29 
     30     private RequestQueue requestQueue;
     31     private String mGeturl,mPosturl,mjSonurl;
     32     private TextView textView;
     33 
     34     @Override
     35     protected void onCreate(Bundle savedInstanceState) {
     36         super.onCreate(savedInstanceState);
     37         setContentView(R.layout.activity_main);
     38         requestQueue = Volley.newRequestQueue(this);
     39         textView = (TextView) findViewById(R.id.tv_id);
     40         findViewById(R.id.but_id).setOnClickListener(this);
     41         findViewById(R.id.but_postid).setOnClickListener(this);
     42         findViewById(R.id.but_jsonid).setOnClickListener(this);
     43         mGeturl = "http://192.168.40.194/qiantai/admin/";
     44         mPosturl = "http://192.168.40.194/qiantai/admin/login_check.php";
     45         mjSonurl = "http://192.168.40.194/mimixun/";
     46     }
     47 
     48     @Override
     49     public void onClick(View view) {
     50         switch (view.getId()){
     51             case R.id.but_id:
     52                 textView.setText("");
     53                 Workrequest();
     54                 break;
     55             case R.id.but_postid:
     56                 textView.setText("");
     57                 PostWorkrequest();
     58                 break;
     59             case R.id.but_jsonid:
     60                 textView.setText("");
     61                 JSonWorkrequest();
     62                 break;
     63         }
     64     }
     65     /**
     66      * Get请求网络数据
     67      */
     68     private void Workrequest() {
     69         StringRequest stringRequest = new StringRequest(mGeturl, new Response.Listener<String>() {
     70             @Override
     71             public void onResponse(String response) {
     72                 textView.append(response);
     73             }
     74         }, new Response.ErrorListener() {
     75             @Override
     76             public void onErrorResponse(VolleyError error) {
     77                 textView.setText("请求错误");
     78             }
     79         }) {
     80             @Override
     81             protected Response<String> parseNetworkResponse(NetworkResponse response) {
     82                 String str = null;
     83                 try {
     84                     str = new String(response.data, "UTF-8");
     85                 } catch (UnsupportedEncodingException e) {
     86                     e.printStackTrace();
     87                 }
     88                 return Response.success(str,
     89                         HttpHeaderParser.parseCacheHeaders(response));
     90             }
     91         };
     92         requestQueue.add(stringRequest);
     93     }
     94     /**
     95      * 请求POST请求
     96      */
     97     private void PostWorkrequest() {
     98         StringRequest request = new StringRequest(Request.Method.POST, mPosturl, new Response.Listener<String>() {
     99             @Override
    100             public void onResponse(String response) {
    101                 try {
    102                     String responsee = new String(response.getBytes("gb2312"),"utf-8");
    103                     textView.append(response);
    104                 } catch (UnsupportedEncodingException e) {
    105                     e.printStackTrace();
    106                 }
    107             }
    108         }, new Response.ErrorListener() {
    109             @Override
    110             public void onErrorResponse(VolleyError error) {
    111                 textView.append("请求错误了");
    112             }
    113         }) {
    114             @Override
    115             protected Map<String, String> getParams() throws AuthFailureError {
    116                 Map<String, String> map = new HashMap<>();
    117                 map.put("username", "admin");
    118                 map.put("userpass", "12346");
    119                 return map;
    120             }
    121         };
    122         requestQueue.add(request);
    123     }
    124 
    125     private void JSonWorkrequest(){
    126         JsonArrayRequest jsonarr = new JsonArrayRequest(mjSonurl, new Response.Listener<JSONArray>() {
    127             @Override
    128             public void onResponse(JSONArray response) {
    129                 textView.append(response.toString());
    130             }
    131         }, new Response.ErrorListener() {
    132             @Override
    133             public void onErrorResponse(VolleyError error) {
    134                 textView.append("请求错误了啊");
    135             }
    136         });
    137         requestQueue.add(jsonarr);
    138     }
    139 }

    Xml文件

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     tools:context="com.wuxianedu.avollys.MainActivity">
     7 
     8     <Button android:id="@+id/but_id"
     9         android:layout_width="wrap_content"
    10         android:layout_height="wrap_content"
    11         android:text="Get请求" />
    12 
    13     <Button android:id="@+id/but_postid" android:layout_toRightOf="@+id/but_id"
    14         android:layout_width="wrap_content"
    15         android:layout_height="wrap_content"
    16         android:text="POSt请求" />
    17 
    18     <Button android:id="@+id/but_jsonid" android:layout_toRightOf="@+id/but_postid"
    19         android:layout_width="wrap_content"
    20         android:layout_height="wrap_content"
    21         android:text="JSOn请求" />
    22     <TextView android:id="@+id/tv_id" android:layout_below="@+id/but_id"
    23         android:layout_width="match_parent"
    24         android:layout_height="match_parent" />
    25 </RelativeLayout>
  • 相关阅读:
    Rsync命令参数详解
    mysql 主从同步如何 把从数据的版本升级到指定的版本
    MySQL同步故障:" Slave_SQL_Running:No" 两种解决办法 (转)
    Linux VPS/服务器 网站及数据库自动本地备份并FTP上传备份脚本
    在kloxo中把不带www的域名做301到带www的域名
    mysql sorce 导入数据乱码问题解决
    linux后台运行和关闭SSH运行,查看后台任务
    centos下MySQL主从同步配置
    ecshop 无法上传产品图片 服务环境少了GD库
    EOJ2902 Contest
  • 原文地址:https://www.cnblogs.com/langfei8818/p/6071587.html
Copyright © 2020-2023  润新知