看了一下volley框架的简单demo,发现使用后,如获取Json文件之类的,所写的代码非常的简单
如
public class JsonRequestAcitivity extends Activity implements OnClickListener,Listener<JSONObject>,ErrorListener{显而易见,TextView中的setText方法并无直接的调用,而数据之间传递和交互只在那句简单的
private Button requestBtn;
private TextView dispText;
RequestQueue rq;
Request request;
private static final String URL = "http://httpbin.org/get?param1=hello";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json_request);
requestBtn = (Button) findViewById(R.id.requestBtn);
dispText = (TextView) findViewById(R.id.dispText);
requestBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
dispText.setText("请求中...");
doJsonRequest();
}
private void doJsonRequest() {
//第三个参数
//A JSONObject to post with the request. Null is allowed and indicates no parameters will be posted along with request.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,URL,null, JsonRequestAcitivity.this,JsonRequestAcitivity.this);
jsonObjectRequest.setTag(JsonRequestAcitivity.class.getSimpleName());//设置tag callAll的时候使用
VolleyTool.getInstance(this).getmRequestQueue().add(jsonObjectRequest);
}
@Override
public void onResponse(JSONObject response) {
//success
dispText.setText("Success::"+response.toString());
}
@Override
public void onErrorResponse(VolleyError error) {
dispText.setText("Fail::"+error.toString());
}
@Override
protected void onDestroy() {
super.onDestroy();
VolleyTool.getInstance(this).getmRequestQueue().cancelAll(JsonRequestAcitivity.class.getSimpleName());
}
}
VolleyTool.getInstance(this).getmRequestQueue().add(jsonObjectRequest);
首先是实例化一个VolleyTool,然后获取一个请求队列,然后将jsonObjectRequest放入队列之中执行;
而VolleyTool文件中,最关键的一句便是
mRequestQueue = Volley.newRequestQueue(context);
public class VolleyTool {private static VolleyTool mInstance = null; private RequestQueue mRequestQueue; private ImageLoader mImageLoader; private VolleyTool(Context context) { mRequestQueue = Volley.newRequestQueue(context); mImageLoader = new ImageLoader(mRequestQueue, new BitmapCache()); } public static VolleyTool getInstance(Context context){ if(mInstance == null){ mInstance = new VolleyTool(context); } return mInstance; } public RequestQueue getmRequestQueue() {return mRequestQueue;}public ImageLoader getmImageLoader() {return mImageLoader;}public void release() {this.mImageLoader = null;this.mRequestQueue = null;mInstance = null;}}因为这一句话,直接从Volley.jar包中调用了volley的这个类,从而获取里面的方法。
public class Volley {而后,从代码中显而易见的是queue.start()方法的执行,就可以从RequestQueue中的networkparseResponse()方法,获取真正的response并实例化。
/** Default on-disk cache directory. */
private static final String DEFAULT_CACHE_DIR = "volley";
/**
* Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
*
* @param context A {@link Context} to use for creating the cache dir.
* @param stack An {@link HttpStack} to use for the network, or null for default.
* @return A started {@link RequestQueue} instance.
*/
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
String userAgent = "volley/0";
try {
String packageName = context.getPackageName();
PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
userAgent = packageName + "/" + info.versionCode;
} catch (NameNotFoundException e) {
}
if (stack == null) {
if (Build.VERSION.SDK_INT >= 9) {
stack = new HurlStack();
} else {
// Prior to Gingerbread, HttpUrlConnection was unreliable.
// See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
}
}
Network network = new BasicNetwork(stack);
RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
queue.start();
return queue;
}
/**
* Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
*
* @param context A {@link Context} to use for creating the cache dir.
* @return A started {@link RequestQueue} instance.
*/
public static RequestQueue newRequestQueue(Context context) {
return newRequestQueue(context, null);
}
}
若是自己也下载了这个volley.jar,便可以知道,实例化时所用的构造方法中,调用了success()方法,从而间接的实现了文本的获取。
-总结:
- 要活用eclipse中的search功能,一步一步的解析问题,把问题简单话。
-不懂解压包里面的文件就解压出来,一步步看,还可以改写。