网络判断+数据拼接

网络判断

       public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected())
            {
                // 当前网络是连接的
                if (info.getState() == NetworkInfo.State.CONNECTED)
                {
                    // 当前所连接的网络可用
                   Toast.makeText(context,"当前所连接的网络可用",Toast.LENGTH_SHORT).show();
                   // EventBus.getDefault().postSticky(new MessageEvent("当前所连接的网络可用"));
                    return true;
                }
            }
        }
        Toast.makeText(context,"网络不可用",Toast.LENGTH_SHORT).show();
       // EventBus.getDefault().postSticky(new MessageEvent("网络不可用"));
        return false;
    }

数据拼接

初始化

 public class IApplication extends Application {

    public static IGetDataBase iGetDataBase;
    public static DaoSession session;

    @Override
    public void onCreate() {
        super.onCreate();


        Fresco.initialize(this);
        Retrofit retrofit =  new Retrofit.Builder().baseUrl("http://gank.io")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        iGetDataBase = retrofit.create(IGetDataBase.class);


        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"test");

        Database database =  helper.getWritableDb();


        session = new DaoMaster(database).newSession();

    }
}

Get、Post拼接

//http://v.juhe.cn/toutiao/index?type="+data+"&key=2f092bd9ce76c0257052d6d3c93c11b4
//http://gank.io/api/data/福利/10/1
public interface IGetDataBase {
    @GET("api/data/福利/{key}/1")
    Call<Bean> get(@Path("key") int key);
/**
 * post请求
 *
 */
@FormUrlEncoded
    @POST("/api/data/福利")
    Call<Bean> post(@Field("key") String key);
}

M层

public class MainModelImpl {
    /**
     * get 请求
     * @param callBack
     */
    public void getData(final ModelCallBack callBack){

       Call<Bean> call =  IApplication.iGetDataBase.get(10);

        call.enqueue(new Callback<Bean>() {
            @Override
            public void onResponse(Call<Bean> call, Response<Bean> response) {

              Bean bean =   response.body();
                callBack.onSuccess(bean);

                // 保存到数据库
                IApplication.session.getResultsBeanDao().insertInTx(bean.getResults());
            }

            @Override
            public void onFailure(Call<Bean> call, Throwable t) {
                callBack.onFailure(new Exception(""));
            }
        });

    }


    /**
     * post 请求
     */
   /* public void postData(final ModelCallBack callBack){

       Call<Bean> call =  IApplication.iGetDataBase.post("/10/1");

       call.enqueue(new Callback<Bean>() {
           @Override
           public void onResponse(Call<Bean> call, Response<Bean> response) {
               Bean bean = response.body() ;
                callBack.onSuccess(bean);

           }

           @Override
           public void onFailure(Call<Bean> call, Throwable t) {

               callBack.onFailure(new Exception(""));
           }
       });

    }*/

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值