我的第一个开源App(彩票开奖查询)

源码:https://github.com/feimengwang/lottery

这个App其实2年前就写好了,那时候是抓取的网页里面的内容,也没有用第三方开源
框架,写的也比较乱,最近没事的时候发现360有接口,返回JSON数据,所以重写了一次,这次也有很多不足,正在修正中。。。。

用到的第三方库
compile 'com.google.code.gson:gson:2.6.2'
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0'

先上图片(图片有点大)



读取网络数据用Rxjava+retrofit

public interface LotteryService {

    @GET("qkaijiang?r=1458219747840")
    Observable<Lottery> geLastData360();

    @GET("qkj")
    Observable<LotteryDetail> getLotteryDetail(@Query("lotId") String lotId, @Query("issue") String issue);
    @GET("qkjlist")
    Observable<LotteryHistory> geLotteryHistory(@Query("lotId") String lotId, @Query("page") String page);
}

 

 

下面就是create个retrofit 来取得数据了

        retrofit = new Retrofit.Builder()
                .baseUrl(LotteryConstant.RETROFIT_BASE_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
        service = retrofit.create(LotteryService.class);
        service.geLastData360()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<Lottery, List>() {
                    @Override
                    public List call(Lottery lottery) {
                        return getLotteryList(lottery);
                    }
                }).subscribe(subscriber);

 

 

其中遇到Toolbar的Overflow menu 没有图标,网上找了一些方案,但是不生效,最后简单重写了Toolbar,终于可以了

public class LotteryToolbar  extends Toolbar{
    public LotteryToolbar(Context context) {
        super(context);
    }

    public LotteryToolbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LotteryToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void inflateMenu(@MenuRes int resId) {
        Menu m = getMenu();
        if(m!=null &&m.getClass()== MenuBuilder.class){
            try {
                Method method = m.getClass().getDeclaredMethod("setOptionalIconsVisible",Boolean.TYPE);
                method.setAccessible(true);
                method.invoke(m,true);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        super.inflateMenu(resId);
    }
}

 

 

就写这么多吧,,,

 

文章首次发布到http://www.jianshu.com/p/d0e964da8767

 

posted @ 2016-03-30 14:45  feimeng0530  阅读(388)  评论(1编辑  收藏  举报