Nohttp网络请求数据,Post以及Get的简单实用以及设置缓存文字的的请求
2016-07-28 16:24
579 查看
开局声明:这是基于nohttp1.0.4-include-source.jar版本写的教程
由于nohttp功能强悍,因此需要多种权限,仅仅一个联网的权限是不够的,如果只给了Internet的权限,去请求网络将还会报错:
onFailed: com.yolanda.nohttp.error.NetworkError: The network is not available, please check the network. The requested url is: http://www.sciencenet.cn/xml/iphoneinterface.aspx?type=news&nums=20
因此建议,直接把nohttp的权限全部加入:
首先是初始化整个应用全局的
Get请求方式
post方式请求
//缓存文字的请求
由于nohttp功能强悍,因此需要多种权限,仅仅一个联网的权限是不够的,如果只给了Internet的权限,去请求网络将还会报错:
onFailed: com.yolanda.nohttp.error.NetworkError: The network is not available, please check the network. The requested url is: http://www.sciencenet.cn/xml/iphoneinterface.aspx?type=news&nums=20
因此建议,直接把nohttp的权限全部加入:
<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" />
首先是初始化整个应用全局的
package com.qg.lizhanqi.nohttpdemo; import android.app.Application; import com.yolanda.nohttp.NoHttp; /** * Created by lizhanqi on 2016-7-28-0028. */ public class MyApplication extends Application { @Override public void onCreate() { //对你没看错就是这么一行就这么简单,NOhttp就是这么简单 NoHttp.initialize(this); super.onCreate(); } }
Get请求方式
public void noHttpGetString(String url) { //第一步:创建Nohttp请求对列(如果是本类使用的比较频繁,在onCreate的时候初始化一次就行了,这里是为了怕忘记这个步骤) requestQueues = NoHttp.newRequestQueue(); //第二步:创建请求对象(url是请求路径) Request<String> stringRequest = NoHttp.createStringRequest(url,RequestMethod.GET);//这里 RequestMethod.GET可以不写(删除掉即可),默认的是Get方式请求 //第三步:加入到请求对列中,requestQueues.add()分别是请求列的请求标志,请求对象,监听回调 requestQueues.add(1, stringRequest, new SimpleResponseListener<String>() { @Override public void onSucceed(int i, Response<String> response) { Toast.makeText(MainActivity.this, "noHttpGetString请求成功" + response, Toast.LENGTH_SHORT).show(); } @Override public void onFailed(int i, String s, Object o, Exception e, int i1, long l) { Toast.makeText(MainActivity.this, "noHttpGetString请求失败" + e, Toast.LENGTH_SHORT).show(); Log.e(TAG, "onFailed: " + e); } }); }
post方式请求
public void noHttpPostString(String url) { //第一步:创建Nohttp请求对列(如果是本类使用的比较频繁,在onCreate的时候初始化一次就行了,这里是为了怕忘记这个步骤) requestQueues = NoHttp.newRequestQueue(); //第二步:创建请求对象(url是请求路径, RequestMethod.POST是请求方式) Request<String> stringPostRequest = NoHttp.createStringRequest(url, RequestMethod.POST); // 添加请求参数例如"http://www.sciencenet.cn/xml/iphoneinterface.aspx?type=news&nums=20" stringPostRequest.add("type", "news"); stringPostRequest.add("nums", "20"); //第三步:加入到请求对列中,requestQueues.add()分别是请求列的请求标志,请求对象,监听回调 requestQueues.add(2, stringPostRequest, new SimpleResponseListener<String>() { @Override//请求成功的回调 public void onSucceed(int i, Response<String> response) { Log.i(TAG, "onSucceed: " + response); Toast.makeText(MainActivity.this, "noHttpPostString请求成功" + response.get(), Toast.LENGTH_LONG).show(); } @Override//请求失败的回调 public void onFailed(int i, String s, Object o, Exception e, int i1, long l) { Log.e(TAG, "onFailed: " + e); } }); }
//缓存文字的请求
public void noHttpCacheString(String url) { //第一步:创建Nohttp请求对列(如果是本类使用的比较频繁,在onCreate的时候初始化一次就行了,这里是为了怕忘记这个步骤) requestQueues = NoHttp.newRequestQueue(); //第二步:创建请求对象(url是请求路径, RequestMethod.POST是请求方式) Request<String> stringPostRequest = NoHttp.createStringRequest(url); //第三步:设置请求缓存的五种模式: //DEFAULT是http标准协议的缓存 //stringPostRequest.setCacheMode(CacheMode.DEFAULT); //REQUEST_NETWORK_FAILED_READ_CACHE请求失败返回上次缓存的数据(建议使用这种) stringPostRequest.setCacheMode(CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE); //NONE_CACHE_REQUEST_NETWORK在没有缓存再去请求网络 // stringPostRequest.setCacheMode(CacheMode.NONE_CACHE_REQUEST_NETWORK); // ONLY_READ_CACHE仅仅请求缓存,如果没有缓存就会请求失败 //stringPostRequest.setCacheMode(CacheMode.ONLY_READ_CACHE); //ONLY_REQUEST_NETWORK仅仅请求网络不支持302重定向 // stringPostRequest.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK); // 添加请求参数例如"http://www.sciencenet.cn/xml/iphoneinterface.aspx?type=news&nums=20" //第三步:加入到请求对列中,requestQueues.add()分别是请求列的请求标志,请求对象,监听回调 requestQueues.add(3, stringPostRequest, new SimpleResponseListener<String>() { @Override//请求成功的回调 public void onSucceed(int i, Response<String> response) { Log.i(TAG, "onSucceed: " + response); Toast.makeText(MainActivity.this, "noHttpCacheString请求成功" + response.get(), Toast.LENGTH_LONG).show(); } @Override//请求失败的回调 public void onFailed(int i, String s, Object o, Exception e, int i1, long l) { Log.e(TAG, "noHttpCacheString..onFailed: " + e); } }); }
相关文章推荐
- Forward和Redirect的比较(http转发请求的原理)
- HTTPClient4.3的典型小例子
- TCP粘包问题
- okhttputil的使用
- (OK)(OK) mptcp_android-x86-kernel-4.4.12.txt
- okHttp的使用
- (转载)CloseableHttpClient设置Timeout
- TCP/IP协议简介(五) 之 应用层
- Linux-(C)网络编程学习socket(初步)
- 利用httpclient 模拟登录,获取登录后信息数据
- 网络连接的判断
- 【精】TCP/IP协议简介(四) 之 传输层UDP&TCP
- VisualSVN Server HTTPS
- 关于HttpUrlConnection+Xpull解析
- TCP/IP协议简介(三) 之 网络层
- Android 关于HttpClient上传中文乱码的解决办法
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- TCP/IP协议简介(二) 之 链路层介绍
- 有关REST的一点学习笔记
- 网络流24题——软件补丁问题(spfa+位运算)