標籤:
1、StringRequest用法
主要分為3步:
(1)、執行個體化一個RequestQueue對象
(2)、設定StringRequest對象參數,並將StringRequest對象加入RequestQueue隊列
(3)、執行start()方法
public class StringActivity extends Activity { private TextView mTvShow; private RequestQueue requestQueue; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_string); initView(); } public void initView(){ mTvShow = (TextView) findViewById(R.id.tv_string); requestQueue = Volley.newRequestQueue(getBaseContext()); requestQueue.add(stringRequest); requestQueue.start(); } /** StringRequest請求,預設為GET*/ public StringRequest stringRequest = new StringRequest("http://www.baidu.com", new Listener<String>() { @Override public void onResponse(String response) { // TODO Auto-generated method stub Toast.makeText(getBaseContext(), response, Toast.LENGTH_SHORT).show(); } }, new ErrorListener(){ @Override public void onErrorResponse(VolleyError error) { // TODO Auto-generated method stub Log.e("StringRequest", error.toString()); } }); /** StringRequest的Post請求,但是需要重寫匿名類getParams()*/ StringRequest stringRequest1 = new StringRequest(Method.POST, "http://www.baidu.com", new Listener<String>() { @Override public void onResponse(String response) { // TODO Auto-generated method stub mTvShow.setText(response); } }, new ErrorListener(){ @Override public void onErrorResponse(VolleyError error) { // TODO Auto-generated method stub Log.e("StringRequest", error.toString()); } }){ // 需要重寫擷取參數的函數 protected Map<String,String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<String, String>(); map.put("wd", "開始嗎"); map.put("rsv_spt", "不開始啦"); return map; }; };}
2、不要忘記加入網路存取權限
<uses-permission android:name="android.permission.INTERNET"/>
3、參考博文:
http://blog.csdn.net/guolin_blog/article/details/17482095/
http://blog.csdn.net/xyz_lmn/article/details/12165391
4、第一次寫部落格,有什麼不足的問題大家多提意見!
Android網路開發之Volley--Volley基本用法(一)