网络连接——Volley
来源:互联网 发布:sql截取字符串 编辑:程序博客网 时间:2024/06/05 08:53
AsyncHttpClient,它把HTTP所有的通信细节全部封装在了内部,我们只需要简单调用几行代码就可以完成通信操作了。Universal-Image-Loader,它使得在界面上显示网络图片的操作变得极度简单,不用关心如何从网络上获取图片,也不用关心开启线程、回收图片资源等细节,Universal-Image-Loader已经把一切都做好了。
Volley可是说是把AsyncHttpClient和Universal-Image-Loader的优点集于了一身,既可以像AsyncHttpClient一样非常简单地进行HTTP通信,也可以像Universal-Image-Loader一样轻松加载网络上的图片。除了简单易用之外,Volley在性能方面也进行了大幅度的调整,它的设计目标就是非常适合去进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,比如说下载文件等,Volley的表现就会非常糟糕。
写一个简单的布局将网络连接返回到信息显示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/button_volley" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Volley网络连接"/> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text_volley" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView></LinearLayout>
然后在MainActivity中创建Volley
package com.example.administrator.myvolley;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import com.android.volley.Request;import com.android.volley.RequestQueue;import com.android.volley.Response;import com.android.volley.VolleyError;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;public class MainActivity extends Activity { private Button mButtonVolley; private TextView mTextViewVolley; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonVolley= (Button) findViewById(R.id.button_volley); mTextViewVolley= (TextView) findViewById(R.id.text_volley); mButtonVolley.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { RequestQueue queue= Volley.newRequestQueue(getApplicationContext()); //RequestQueue是一个请求队列对象,它可以缓存所有的HTTP请求,然后按照一定的算法并发地发出这些请求。 StringRequest request=new StringRequest(Request.Method.GET, "http://www.ncwu.edu.cn", new Response.Listener<String>() { @Override public void onResponse(String response) { mTextViewVolley.setText(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getApplicationContext(),"网络连接失败",Toast.LENGTH_LONG).show(); } }); //这里new出了一个StringRequest对象,StringRequest的构造函数需要传入四个参数, // 第一个参数是请求的方法,第二个参数就是目标服务器的URL地址,第三个参数是服务器响应成功的回调,第四个参数是服务器响应失败的回调。 // 其中,目标服务器地址我们填写的是百度的首页,然后在响应成功的回调里打印出服务器返回的内容,在响应失败的回调里返回一个Toast。 queue.add(request); //将这个StringRequest对象添加到RequestQueue里面 } }); }}
最后需要在Manifest中添加权限
<uses-permission android:name="android.permission.INTERNET"/>
RequestQueue内部的设计就是非常合适高并发的,因此我们不必为每一次HTTP请求都创建一个RequestQueue对象,这是非常浪费资源的,基本上在每一个需要和网络交互的Activity中创建一个RequestQueue对象就足够了。所以在这里使用单例模式来使得代码更加完善
package com.example.administrator.myvolley;import android.content.Context;import android.graphics.Bitmap;import android.util.LruCache;import com.android.volley.Request;import com.android.volley.RequestQueue;import com.android.volley.toolbox.ImageLoader;import com.android.volley.toolbox.Volley;/** * Created by Administrator on 2015/9/14. */public class MySingleton { private static MySingleton mInstance; private RequestQueue mRequestQueue; private static Context mCtx; private MySingleton(Context context) { mCtx = context; } public static synchronized MySingleton getInstance(Context context) { if (mInstance == null) { mInstance = new MySingleton(context); } return mInstance; } public RequestQueue getRequestQueue() { if (mRequestQueue == null) { //如果请求队列为空的话再进行创建 mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); } return mRequestQueue; } }
0 0
- 网络连接——Volley
- Androdi网络连接——WebView、Volley、xutils
- 网络——Volley
- android网络连接Volley
- Volley网络连接封装类
- Android-使用Volley 连接网络
- Android网络连接---Google网络库Volley
- 网络通信框架——Volley(一)
- Android 网络通信——Volley
- Android —— 网络请求框架 Volley
- HTTP网络通信框架——Volley
- Android网络图片下载框架—Volley
- 第三十八天 网络连接:Volley、xUtils
- 网络连接之HttpClient,Volley,xUtils
- 网络连接之——谷歌提供的通信框架Volley【避免创建多个线程对象】
- 谷歌Volley网络框架讲解——网络枢纽
- 谷歌Volley网络框架讲解——网络枢纽
- android网络开源框架volley(五)——volley的一些细节
- 【Java多线程】-Callable,Future,FutureTask
- Android中让多个线程顺序执行探究
- sex==0?0:1; 出错
- Windows和Linux远程复制文件——pscp和scp
- iOS Label添加删除线
- 网络连接——Volley
- Java基本知识之二
- SAX方式读取XML文件
- 获取IP地址
- Android版本(1.0-7.0)
- js写的闹钟,支持多个闹钟
- Usaco 4.3.1 Buy Low, Buy Lower 逢低吸纳详细解题报告
- 2014-2015学年总结-跬步千里
- 时间工具类