标签:
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); }
标签:
原文地址:http://www.cnblogs.com/1ming/p/5586221.html