Gson封装。
public class GsonUtils {
public static < T> T getObject(String jsonString ,Class< T> tClass){
T t= null;
try {
Gson gson= new Gson() ;
t=gson.fromJson(jsonString ,tClass) ;
} catch (Exception e){
e.printStackTrace() ;
}
return t ;
}
public static < T> List< T> getObjects(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 ;
}
}
public static < T> T getObject(String jsonString ,Class< T> tClass){
T t= null;
try {
Gson gson= new Gson() ;
t=gson.fromJson(jsonString ,tClass) ;
} catch (Exception e){
e.printStackTrace() ;
}
return t ;
}
public static < T> List< T> getObjects(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() ;
}
}
@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) ;
ImageLoader. getInstance().init(configuration) ;
3、在需要加载图片的地方使用两个方法。
1)新建实例
final ImageLoader imageLoader=ImageLoader.
getInstance()
;
2)加载参数设置 在这里可以设置图片的参数
DisplayImageOptions options=
new DisplayImageOptions.Builder()
.cacheInMemory( true)
.cacheOnDisk( true)
.build() ;
.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> T getObject(String jsonString ,Class< T> tClass){
T t= null;
try {
Gson gson= new Gson() ;
t=gson.fromJson(jsonString ,tClass) ;
} catch (Exception e){
e.printStackTrace() ;
}
return t ;
}
public static < T> List< T> getObjects(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 ;
}
}
public static < T> T getObject(String jsonString ,Class< T> tClass){
T t= null;
try {
Gson gson= new Gson() ;
t=gson.fromJson(jsonString ,tClass) ;
} catch (Exception e){
e.printStackTrace() ;
}
return t ;
}
public static < T> List< T> getObjects(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 ;
}
}
师兄应该是用了新浪云的云应用。想着自己也弄一个,同时轮播功能也希望这两天可以实现。