Android採用Volley具體的例子展示完整的非同步載入資料(一)

來源:互聯網
上載者:User

標籤:

MainActivity例如下列:
package cc.cn;import java.util.HashMap;import org.json.JSONObject;import android.app.Activity;import android.content.Context;import android.os.Bundle;import com.android.volley.AuthFailureError;import com.android.volley.RequestQueue;import com.android.volley.Response;import com.android.volley.VolleyError;import com.android.volley.Request.Method;import com.android.volley.toolbox.JsonObjectRequest;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;/** * Demo描寫敘述: * 去年一個項目中簡單的使用了Volley,參見: * http://blog.csdn.net/lfdfhl/article/details/12223345 * 近期guolin寫了幾篇文章具體介紹Volley,所以在此比較系統地學習一下. * 在本示範範例中,主要包含: * 1 利用Volley實現Get請求 * 2 利用Volley實現Post請求 * 3 利用Volley實現Json資料請求 *  * 注意事項: * 1 引入jar包volley.jar * 2 網路許可權<uses-permission android:name="android.permission.INTERNET"/> *  * 學習資料: * http://blog.csdn.net/guolin_blog/article/details/17482095 * Thank you very much * */public class MainActivity extends Activity {private RequestQueue mRequestQueue;private StringRequest mStringRequest;private Context mContext;private JsonObjectRequest mJsonObjectRequest;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        volley_get();        //volley_post();        //volley_json();    }            //利用Volley實現Get請求    private void volley_get(){    mContext=this;    String url="http://www.baidu.com/";    //1 建立RequestQueue對象    mRequestQueue=Volley.newRequestQueue(mContext);    //2 建立StringRequest對象mStringRequest = new StringRequest(url,new Response.Listener<String>() {@Overridepublic void onResponse(String response) {                        System.out.println("請求結果:"+response);}}, new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError error) {                        System.out.println("請求錯誤:"+error.toString());}});//3 將StringRequest加入到RequestQueuemRequestQueue.add(mStringRequest);    }        //利用Volley實現Post請求private void volley_post() {String url = "http://hxyj.cwwic.cn/wap/api_cs/user.php?action=login";mContext = this;mRequestQueue = Volley.newRequestQueue(mContext);mStringRequest = new StringRequest(Method.POST, url,new Response.Listener<String>() {@Overridepublic void onResponse(String response) {System.out.println("請求結果:" + response);}}, new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError error) {System.out.println("請求錯誤:" + error.toString());}}) {//攜帶參數@Overrideprotected HashMap<String, String> getParams()throws AuthFailureError {HashMap<String, String> hashMap = new HashMap<String, String>();hashMap.put("un", "411111222");hashMap.put("pw", "180034111");return hashMap;}};mRequestQueue.add(mStringRequest);} //利用Volley實現Json資料請求private void volley_json() {mContext = this;String url = "http://m.weather.com.cn/data/101010100.html";// 1 建立RequestQueue對象mRequestQueue = Volley.newRequestQueue(mContext);// 2 建立JsonObjectRequest對象mJsonObjectRequest = new JsonObjectRequest(url, null,new Response.Listener<JSONObject>() {@Overridepublic void onResponse(JSONObject response) {System.out.println("請求結果:" + response.toString());}}, new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError error) {System.out.println("請求錯誤:" + error.toString());}});// 3 將JsonObjectRequest加入到RequestQueuemRequestQueue.add(mJsonObjectRequest);}            }

main.xml例如以下:

<?xml version="1.0" encoding="utf-8"?

><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="學習Volley(一)" android:layout_centerInParent="true" /></RelativeLayout>



著作權聲明:本文博主原創文章,部落格,未經同意不得轉載。

Android採用Volley具體的例子展示完整的非同步載入資料(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.