之前讲过volley的简介,这章主要涉及调用volley后的回调和一些需要注意的地方。
在讲解volley之前,先说明一个看似题外又不题外的话。
Volley是Google在2003年的I/O大会上推出的通信框架,结合了AsyncHttpClient和Universal-Image-Loader的优点——简化了http的使用 + 异步加载图片的神奇能力。Android中的Http实现主要有HttpUrlConnection和HttpClient两种,关于二者的选择Google在Blog中表示推荐在姜饼小人(API level = 9)及以上的版本中使用Java的HttpUrlConnection而在之前的版本使用Apache的HttpClient,这在Volley这个框架中也有明确的体现。
回过头来说 volley
前提条件:
对于volley的jar包,我是在https://github.com/mcxiaoke/android-volley中下载的。
拿到volley的jar后,将他放入eclipse中的libs,就可以调用volley的接口了。
下面的讲述参考http://www.cnblogs.com/liu37130/p/3902390.html
自己子写了一套工程,用于验证。
附代码:acrivity xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.volleytest.VolleyTest" >
<TextView
android:id="@+id/requestVolley"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/requestVolley" />
<TextView
android:id="@+id/displayResponse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/requestVolley"
android:text="@string/displayResponse" />
</RelativeLayout>
请求 网络类
package com.example.volleytest;
import android.content.Context;
import android.util.Log;
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 RequestVolley {
Context context;
String url;
public RequestVolley(Context context) {
this.context = context;
}
void setUrl(String url) {
this.url = url;
}
void setRequestVolley(final RequestListener listener) {
// 初始化一个请求队列
RequestQueue queue = Volley.newRequestQueue(context);
//String url = "http://www.google.com";
// 根据给定的URL新建一个请求
@SuppressWarnings("unchecked")
StringRequest stringRequest = new StringRequest(Request.Method.GET,
url, new Response.Listener() {
public void onResponse(String response) {
// 在这里操作UI组件是安全的,因为响应返回时这个函数会被post到UI线程来执行
// 在这里尽情蹂躏响应的String。
listener.success(response);
}
@Override
public void onResponse(Object arg0) {
// TODO Auto-generated method stub
if(arg0 instanceof String){
Log.i("request","ResponseString");
onResponse((String)arg0);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// 出错了怎么办?凉拌!并且在这里拌。
Log.i("requesterror","requesterror");
if (error.networkResponse != null) {
int statusCode = error.networkResponse.statusCode;
String messString=error.getMessage();
Log.i("requesterror","statusCode="+statusCode);
Log.i("requesterror","messString="+messString);
}
}
});
// 把这个请求加入请求队列
queue.add(stringRequest);
}
}
activity 类
package com.example.volleytest;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class VolleyTest extends ActionBarActivity implements
View.OnClickListener {
TextView request;
TextView displayResponse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_volley_test);
request=(TextView)findViewById(R.id.requestVolley);
displayResponse=(TextView)findViewById(R.id.displayResponse);
request.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.volley_test, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (arg0 == request) {
RequestVolley requset = new RequestVolley(this);
requset.setUrl("http://www.baidu.com");
requset.setRequestVolley(new RequestListener(){
@Override
public void success(String response) {
// TODO Auto-generated method stub
displayResponse.setText(response);
}
@Override
public void failure() {
// TODO Auto-generated method stub
}
});
}
}
}
网络请求的 回传 接口
package com.example.volleytest;
public interface RequestListener {
void success(String response);
void failure();
}