今天项目需要用到Nohttp,这个相对来说比较好理解,毕竟是国内封装的。
烟花易冷心易碎 的:http://www.cnblogs.com/lizhanqi/p/5715281.html
在使用NoHttp的get方法时,需要传递进入url,和作为队列代号的int类型的 i;
private RequestQueue requestQueue; public void getMyInfo(String url,int i){ requestQueue = NoHttp.newRequestQueue(); RequeststringRequest = NoHttp.createStringRequest(url, RequestMethod.GET); requestQueue.add(i, stringRequest, new SimpleResponseListener () { @Override public void onStart(int what) { Toast.makeText(DynamicActivity.this,"开始",Toast.LENGTH_SHORT).show(); } @Override public void onSucceed(int what, Response response) { String s = response.toString(); Toast.makeText(DynamicActivity.this,s,Toast.LENGTH_SHORT).show(); } @Override public void onFailed(int what, Response response) { Toast.makeText(DynamicActivity.this,"失败"+ what+"返回的响应是"+ response.toString(),Toast.LENGTH_SHORT).show(); } @Override public void onFinish(int what) { Toast.makeText(DynamicActivity.this,"结束",Toast.LENGTH_SHORT).show(); } }); }
POST方法,还没有使用,先把代码copy下:
1 public void noHttpPostString(String url) { 2 //第一步:创建Nohttp请求对列(如果是本类使用的比较频繁,在onCreate的时候初始化一次就行了,这里是为了怕忘记这个步骤) 3 requestQueues = NoHttp.newRequestQueue(); 4 //第二步:创建请求对象(url是请求路径, RequestMethod.POST是请求方式) 5 RequeststringPostRequest = NoHttp.createStringRequest(url, RequestMethod.POST); 6 // 添加请求参数例如"http://www.sciencenet.cn/xml/iphoneinterface.aspx?type=news&nums=20" 7 stringPostRequest.add("type", "news"); 8 stringPostRequest.add("nums", "20"); 9 //第三步:加入到请求对列中,requestQueues.add()分别是请求列的请求标志,请求对象,监听回调 10 requestQueues.add(2, stringPostRequest, new SimpleResponseListener () { 11 @Override//请求成功的回调 12 public void onSucceed(int i, Response response) { 13 Log.i(TAG, "onSucceed: " + response); 14 Toast.makeText(MainActivity.this, "noHttpPostString请求成功" + response.get(), Toast.LENGTH_LONG).show(); 15 } 16 17 @Override//请求失败的回调 18 public void onFailed(int i, String s, Object o, Exception e, int i1, long l) { 19 Log.e(TAG, "onFailed: " + e); 20 } 21 }); 22 }
联网权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />