public interface JieKou {
public void jie(String json );
}
ImageLoaderUtil .init (this );
public static void getDate(Context context , final String path , final JieKou jie ){
if (NeteState .isConn (context )){
AsyncTask <Void ,Void ,String > as =new AsyncTask <Void , Void , String >() {
@Override
protected String doInBackground(Void ... voids ) {
URL url = null ;
try {
url = new URL(path );
HttpURLConnection connection = (HttpURLConnection ) url .openConnection();
connection .setRequestMethod("GET" );
connection .setReadTimeout(5000 );
connection .setConnectTimeout(5000 );
int responseCode = connection .getResponseCode();
if (responseCode ==200 ){
InputStream inputStream = connection .getInputStream();
String json = Strings .streamToString (inputStream , "utf-8" );
return json ;
}
} catch (Exception e ) {
e .printStackTrace();
}
return null ;
}
@Override
protected void onPostExecute(String s ) {
super .onPostExecute(s );
jie .jie(s );
}
};
as .execute();
}else {
NeteState .showNoNetWorkDlg (context );
}
}
然后局可以直接使用了。 NetDate .getDate (getActivity(), "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1" , new JieKou () {
@Override
public void jie(String json ) {
Gson gson =new Gson();
DataDataBean dataDataBean = gson .fromJson(json , DataDataBean .class );
List <DataDataBean.NewslistBean > newslist = dataDataBean .getNewslist();
list .addAll(newslist );
setadpter();
//停止刷新
lv .onRefreshComplete();
}
});