@Yano
2016-01-19T17:02:54.000000Z
字数 3885
阅读 3157
Android
极客学院 Android 主流开源库深度剖析:http://ke.jikexueyuan.com/xilie/108
HTTP 网络数据交互请求,是极其重要和频繁使用的模块,也是最基本的网络编程所需要学习的。学习并掌握一款好的 HTTP 请求框架,对我们的网络开发非常重要。
HTTP 请求在移动网络编程中,主要表现在 GET
和 POST
请求接口数据的使用。
两款优秀的开源 HTTP 网络请求框架:
https://github.com/LjyYano/Android-Open-Source-Lib-Demo/blob/master/VollyDemo/libs/Volley.jar
不适合大数据、流媒体的网络请求。
public class MyApplication extends Application {
public static RequestQueue queue;
@Override
public void onCreate() {
super.onCreate();
queue = Volley.newRequestQueue(getApplicationContext());
}
public static RequestQueue getHttpQueue() {
return queue;
}
}
public class MainActivity extends Activity {
/**
* 1、Volley的Get和Post请求方式的使用
*
* 2、Volley的网络请求队列建立和取消队列请求及Activity周期关联
*
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vollyGet();
// vollyPost();
}
@Override
protected void onStop() {
super.onStop();
MyApplication.getHttpQueue().cancelAll("get");
MyApplication.getHttpQueue().cancelAll("post");
}
private void vollyPost() {
String url = "http://apis.juhe.cn/mobile/get?";
StringRequest request = new StringRequest(Method.POST, url,
new Listener<String>() {
public void onResponse(String arg0) {
Toast.makeText(MainActivity.this, arg0,
Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError arg0) {
Toast.makeText(MainActivity.this, "网络请求失败",
Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> map = new HashMap<String, String>();
map.put("phone", "13666666666");
map.put("key", "335adcc4e891ba4e4be6d7534fd54c5d");
return map;
}
};
// Tag用于寻找
request.setTag("post");
MyApplication.getHttpQueue().add(request);
}
private void vollyGet() {
String url = "http://apis.juhe.cn/mobile/get?phone=15822882820&key=335adcc4e891ba4e4be6d7534fd54c5d";
StringRequest request = new StringRequest(Method.GET, url,
new Listener<String>() {
public void onResponse(String arg0) {
Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG)
.show();
}
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError arg0) {
Toast.makeText(MainActivity.this, "网络请求失败", Toast.LENGTH_LONG)
.show();
}
});
// Tag用于寻找
request.setTag("get");
MyApplication.getHttpQueue().add(request);
}
}
AndroidManifest 文件中,加入 MyApplication 和网络权限。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// asynchttpGet();
asynchttpPost();
}
private void asynchttpPost() {
AsyncHttpClient client = new AsyncHttpClient();
String url = "http://apis.juhe.cn/mobile/get?";
RequestParams params = new RequestParams();
params.put("phone", "13666666666");
params.put("key", "335adcc4e891ba4e4be6d7534fd54c5d");
client.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String arg0) {
super.onSuccess(arg0);
Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG)
.show();
}
@Override
public void onFailure(Throwable arg0) {
super.onFailure(arg0);
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_LONG)
.show();
}
});
}
private void asynchttpGet() {
AsyncHttpClient client = new AsyncHttpClient();
String url = "http://apis.juhe.cn/mobile/get?phone=13666666666&key=335adcc4e891ba4e4be6d7534fd54c5d";
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String arg0) {
super.onSuccess(arg0);
Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG)
.show();
}
@Override
public void onFailure(Throwable arg0) {
Toast.makeText(MainActivity.this, "网络请求失败", Toast.LENGTH_LONG)
.show();
super.onFailure(arg0);
}
});
}
}