Volley框架模式
Posted 尚文韬
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Volley框架模式相关的知识,希望对你有一定的参考价值。
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); }
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); }
以上是关于Volley框架模式的主要内容,如果未能解决你的问题,请参考以下文章