• Nohttp网络请求数据,Post以及Get的简单实用以及设置缓存文字的的请求


    开局声明:这是基于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的权限全部加入:

        <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" />

    首先是初始化整个应用全局的

     1 package com.qg.lizhanqi.nohttpdemo;
     2 
     3 import android.app.Application;
     4 
     5 import com.yolanda.nohttp.NoHttp;
     6 
     7 /**
     8  * Created by lizhanqi on 2016-7-28-0028.
     9  */
    10 public class MyApplication extends Application {
    11     @Override
    12     public void onCreate() {
    13         //对你没看错就是这么一行就这么简单,NOhttp就是这么简单
    14         NoHttp.initialize(this);
    15         super.onCreate();
    16     }
    17 }

    Get请求方式

     1     public void noHttpGetString(String url) {
     2         //第一步:创建Nohttp请求对列(如果是本类使用的比较频繁,在onCreate的时候初始化一次就行了,这里是为了怕忘记这个步骤)
     3         requestQueues = NoHttp.newRequestQueue();
     4         //第二步:创建请求对象(url是请求路径)
     5         Request<String> stringRequest = NoHttp.createStringRequest(url,RequestMethod.GET);//这里 RequestMethod.GET可以不写(删除掉即可),默认的是Get方式请求
     6         //第三步:加入到请求对列中,requestQueues.add()分别是请求列的请求标志,请求对象,监听回调
     7         requestQueues.add(1, stringRequest, new SimpleResponseListener<String>() {
     8             @Override
     9             public void onSucceed(int i, Response<String> response) {
    10                 Toast.makeText(MainActivity.this, "noHttpGetString请求成功" + response, Toast.LENGTH_SHORT).show();
    11             }
    12 
    13             @Override
    14             public void onFailed(int i, String s, Object o, Exception e, int i1, long l) {
    15                 Toast.makeText(MainActivity.this, "noHttpGetString请求失败" + e, Toast.LENGTH_SHORT).show();
    16                 Log.e(TAG, "onFailed: " + e);
    17             }
    18         });
    19     }

    post方式请求

     1     public void noHttpPostString(String url) {
     2         //第一步:创建Nohttp请求对列(如果是本类使用的比较频繁,在onCreate的时候初始化一次就行了,这里是为了怕忘记这个步骤)
     3         requestQueues = NoHttp.newRequestQueue();
     4         //第二步:创建请求对象(url是请求路径, RequestMethod.POST是请求方式)
     5         Request<String> stringPostRequest = 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<String>() {
    11             @Override//请求成功的回调
    12             public void onSucceed(int i, Response<String> 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     }

    //缓存文字的请求

     1     public void noHttpCacheString(String url) {
     2         //第一步:创建Nohttp请求对列(如果是本类使用的比较频繁,在onCreate的时候初始化一次就行了,这里是为了怕忘记这个步骤)
     3         requestQueues = NoHttp.newRequestQueue();
     4         //第二步:创建请求对象(url是请求路径, RequestMethod.POST是请求方式)
     5         Request<String> stringPostRequest = NoHttp.createStringRequest(url);
     6         //第三步:设置请求缓存的五种模式:
     7         //DEFAULT是http标准协议的缓存
     8         //stringPostRequest.setCacheMode(CacheMode.DEFAULT);
     9         //REQUEST_NETWORK_FAILED_READ_CACHE请求失败返回上次缓存的数据(建议使用这种)
    10         stringPostRequest.setCacheMode(CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE);
    11         //NONE_CACHE_REQUEST_NETWORK在没有缓存再去请求网络
    12         // stringPostRequest.setCacheMode(CacheMode.NONE_CACHE_REQUEST_NETWORK);
    13         // ONLY_READ_CACHE仅仅请求缓存,如果没有缓存就会请求失败
    14         //stringPostRequest.setCacheMode(CacheMode.ONLY_READ_CACHE);
    15         //ONLY_REQUEST_NETWORK仅仅请求网络不支持302重定向
    16         // stringPostRequest.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK);
    17         // 添加请求参数例如"http://www.sciencenet.cn/xml/iphoneinterface.aspx?type=news&nums=20"
    18         //第三步:加入到请求对列中,requestQueues.add()分别是请求列的请求标志,请求对象,监听回调
    19         requestQueues.add(3, stringPostRequest, new SimpleResponseListener<String>() {
    20             @Override//请求成功的回调
    21             public void onSucceed(int i, Response<String> response) {
    22                 Log.i(TAG, "onSucceed: " + response);
    23                 Toast.makeText(MainActivity.this, "noHttpCacheString请求成功" + response.get(), Toast.LENGTH_LONG).show();
    24             }
    25 
    26             @Override//请求失败的回调
    27             public void onFailed(int i, String s, Object o, Exception e, int i1, long l) {
    28                 Log.e(TAG, "noHttpCacheString..onFailed: " + e);
    29             }
    30         });
    31     }
  • 相关阅读:
    Docker windows下安装,入门及注意事项,并搭建包含Nodejs的webapp
    360浏览器table中的td为空时td边框不显示的解决方法
    关于发布webservice提示The test form is only available for requests from the local machine
    CRM相关SQl手记
    页面右下角弹出的消息提示框
    MS CRM2011 js常用总结
    MVC razor 使用服务器控件
    常用正则表达式
    CRM 2011 常用对象
    人工智能AI-机器视觉CV-数据挖掘DM-机器学习ML-神经网络-[资料集合贴]
  • 原文地址:https://www.cnblogs.com/lizhanqi/p/5715281.html
Copyright © 2020-2023  润新知