码迷,mamicode.com
首页 > 其他好文 > 详细

Volley框架模式

时间:2016-06-15 09:20:23      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

Volley-GET

技术分享
import com.android.volley.toolbox.Volley;


public class JDKActivity extends AppCompatActivity {

    EditText et1;
    RequestQueue requestQueue;
    //请求队列
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jdk);
        et1=(EditText)findViewById(R.id.et1);
        //创建Volley的请求队列
        //在子线程
        requestQueue = Volley.newRequestQueue(this);
    }
    //Volley-GET
    public void bt_3(View v)
    {
        //1-启动进度对话框
        final ProgressDialog pd = ProgressDialog.show(this,null,"请稍后……");

        //构建StringRequest
        StringRequest sr=new StringRequest("http://192.168.0.164:81/index.asp?name=Volley&password=456",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        //处理正常响应
                        //在主线程里运行
                        et1.setText(s);
                        pd.dismiss();
                    }
                },
                new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                //处理异常响应
                pd.dismiss();
                Toast.makeText(JDKActivity.this, "响应异常   响应状态码"+volleyError.networkResponse.statusCode, Toast.LENGTH_SHORT).show();
            }
        });
        //加入队列
        requestQueue.add(sr);
    }
View Code

 

Volley-POST

技术分享
 //Volley-POST
    public void bt_4(View v)
    {
        //1-启动进度对话框
        final ProgressDialog pd = ProgressDialog.show(this,null,"请稍后……");

        //构建StringRequest
        StringRequest sr=new StringRequest(Request.Method.POST,"http://192.168.0.164:81/index.asp",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        //处理正常响应
                        //在主线程里运行
                        et1.setText(s);
                        pd.dismiss();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        //处理异常响应
                        pd.dismiss();
                        Toast.makeText(JDKActivity.this, "响应异常   响应状态码"+volleyError.networkResponse.statusCode, Toast.LENGTH_SHORT).show();
                    }
                }){
            //重写得到参数的方法
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> rtn=new HashMap<>();
                rtn.put("name","postvolley");
                rtn.put("password","567");
                return rtn;
            }
        };
        //加入队列
        requestQueue.add(sr);
    }
View Code

技术分享

Volley框架模式

标签:

原文地址:http://www.cnblogs.com/1ming/p/5586221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!