reader(四)

Gson封装。
public class GsonUtils  {

    public static < T getObject(String jsonString ,Class< T> tClass){
        t= null;
        try {
            Gson gson= new Gson() ;
            t=gson.fromJson(jsonString ,tClass) ;
        } catch (Exception e){
            e.printStackTrace() ;
        }
        return t ;
    }

    public static < T> List< TgetObjects(String jsonString Class< T> cls){
        List< T> list= new ArrayList<>() ;
        try{
            Gson gson= new Gson() ;
            list=gson.fromJson(jsonString ,new TypeToken<List< T>>(){}.getType()) ;
        } catch (Exception e){
            e.printStackTrace() ;
        }
        return list ;
    }

}


监听事件后跳转到每一篇文章的具体内容。
StoryDetailActivity

public class StoryDetailActivity  extends AppCompatActivity {

    @Bind(R.id. iv_story_image)
    ImageView  iv_story_image ;
    @Bind(R.id. toolBar_story)
    Toolbar  mToolbar ;
    @Bind(R.id. collapsingToolbarLayout_story)
    CollapsingToolbarLayout  mCollapsingToolbarLayout ;
    @Bind(R.id. appBarLayout_story)
    AppBarLayout  mAppBarLayout ;
    @Bind(R.id. wv_story_body)
    WebView  wv_story_body ;
    @Bind(R.id. coordinatorLayout_story)
    CoordinatorLayout  mCoordinatorLayout ;

    private StorySimple  mStorySimple ;
    private StoryDetail  mStoryDetail ;
    private Handler  mHandler= new Handler() ;

    @Override
    protected void  onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState) ;
        setContentView(R.layout. activity_story_detail) ;
        ButterKnife. bind( this) ;
        initData() ;
        initView() ;
    }

    private void  initData() {
        mStorySimple=getIntent().getParcelableExtra( "story_simple") ;
    }

    private void  initView() {
        setSupportActionBar( mToolbar) ;
        getSupportActionBar().setDisplayHomeAsUpEnabled( true) ;
        mToolbar.setNavigationOnClickListener( new View.OnClickListener() {
            @Override
            public void  onClick(View v) {
                onBackPressed() ;
            }
        }) ;

        mCollapsingToolbarLayout.setTitle( mStorySimple.getTitle()) ;
        wv_story_body.getSettings().setJavaScriptEnabled( true) ;
        wv_story_body.getSettings().setCacheMode(WebSettings. LOAD_CACHE_ELSE_NETWORK) ;
        wv_story_body.getSettings().setDomStorageEnabled( true) ;
        wv_story_body.getSettings().setAppCacheEnabled( true) ;
        wv_story_body.getSettings().setDatabaseEnabled( true) ;


        if (NetWorkUtils. isNetWorkConnected( this)){
            MyHttpClient. httpGet(MyHttpUrl. DAILY_BASEURL mStorySimple.getId() , new Callback() {
                @Override
                public void  onFailure(Call call IOException e) {

                }

                @Override
                public void  onResponse(Call call Response response)  throws IOException {
                    mStoryDetail= GsonUtils. getObject(response.body().string() ,StoryDetail. class) ;
                    mHandler.post( new Runnable() {
                        @Override
                        public void  run() {
                          final ImageLoader imageLoader=ImageLoader. getInstance() ;
                            DisplayImageOptions options= new DisplayImageOptions.Builder()
                                    .cacheInMemory( true)
                                    .cacheOnDisk( true)
                                    .build() ;
                            imageLoader.displayImage( mStoryDetail.getImage() , iv_story_image ,options) ;
                            String css =  "<link rel= \" stylesheet \"  href= \" file:///android_asset/css/news.css \"  type= \" text/css \" >" ;
                            String html =  "<html><head>" + css +  "</head><body>" mStoryDetail.getBody() +  "</body></html>" ;
                            html = html.replace( "<div class= \" img-place-holder \" >" "") ;
                            wv_story_body.loadDataWithBaseURL( "x-data://base" html "text/html" "UTF-8" , null) ;
                        }
                    }) ;
                }
            }) ;
        }

    }

    @Override
    public void  onBackPressed() {
        super.onBackPressed() ;
        finish() ;
    }
}


这里是UIL框架的使用。并且 在html代码的头部引入css文件。

本人理解的UIL框架的使用:
1、安装或添加依赖:
compile  'com.nostra13.universalimageloader:universal-image-loader:1.9.4'

2、全局配置,Application
ImageLoaderConfiguration configuration=ImageLoaderConfiguration. createDefault( this) ;
ImageLoader. getInstance().init(configuration) ;

3、在需要加载图片的地方使用两个方法。
   1)新建实例
       
final ImageLoader imageLoader=ImageLoader. getInstance() ;

   2)加载参数设置  在这里可以设置图片的参数
     
DisplayImageOptions options= new DisplayImageOptions.Builder()
        .cacheInMemory( true)
        .cacheOnDisk( true)
        .build() ;
   
   3)将设置以及图片放入实例
  
imageLoader.displayImage( mStoryDetail.getImage() , iv_story_image ,options) ;

将CSS文件放入html的头部
使用link标签。标明css文件位置,类型,
String css =  "<link rel= \" stylesheet \"  href= \" file:///android_asset/css/news.css \"  type= \" text/css \" >" ;
String html =  "<html><head>" + css +  "</head><body>" mStoryDetail.getBody() +  "</body></html>" ;


使用泛型与Gson一起解析JSON数据
public class GsonUtils  {

    public static < T getObject(String jsonString ,Class< T> tClass){
        t= null;
        try {
            Gson gson= new Gson() ;
            t=gson.fromJson(jsonString ,tClass) ;
        } catch (Exception e){
            e.printStackTrace() ;
        }
        return t ;
    }

    public static < T> List< TgetObjects(String jsonString Class< T> cls){
        List< T> list= new ArrayList<>() ;
        try{
            Gson gson= new Gson() ;
            list=gson.fromJson(jsonString ,new TypeToken<List< T>>(){}.getType()) ;
        } catch (Exception e){
            e.printStackTrace() ;
        }
        return list ;
    }

}



师兄应该是用了新浪云的云应用。想着自己也弄一个,同时轮播功能也希望这两天可以实现。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值