网络过程及请求结果流转string及解析
2016-08-26 09:20
429 查看
1,请求过程:
public class NewsUtils {public static String newsPath_url = "http://192.168.13.83:8080/itheima74/servlet/GetNewsServlet";
//封装新闻的假数据到list中返回
public static ArrayList<NewsBean> getAllNewsForNetWork(Context context) {
ArrayList<NewsBean> arrayList = new ArrayList<NewsBean>();
try{
//1.请求服务器获取新闻数据
//获取一个url对象,通过url对象得到一个urlconnnection对象
URL url = new URL(newsPath_url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置连接的方式和超时时间
connection.setRequestMethod("GET");
connection.setConnectTimeout(10*1000);
//获取请求响应码
int code = connection.getResponseCode();
if(code == 200){
//获取请求到的流信息
InputStream inputStream = connection.getInputStream();
String result = StreamUtils.streamToString(inputStream);
//2.解析获取的新闻数据到List集合中。
JSONObject root_json = new JSONObject(result);//将一个字符串封装成一个json对象。
JSONArray jsonArray = root_json.getJSONArray("newss");//获取root_json中的newss作为jsonArray对象
for (int i = 0 ;i < jsonArray.length();i++){//循环遍历jsonArray
JSONObject news_json = jsonArray.getJSONObject(i);//获取一条新闻的json
NewsBean newsBean = new NewsBean();
newsBean. id = news_json.getInt("id");
newsBean. comment = news_json.getInt("comment");//评论数
newsBean. type = news_json.getInt("type");//新闻的类型,0 :头条 1 :娱乐 2.体育
newsBean. time = news_json.getString("time");
newsBean. des = news_json.getString("des");
newsBean. title = news_json.getString("title");
newsBean. news_url = news_json.getString("news_url");
newsBean. icon_url = news_json.getString("icon_url");
arrayList.add(newsBean);
}
//3.清楚数据库中旧的数据,将新的数据缓存到数据库中
new NewsDaoUtils(context).delete();
new NewsDaoUtils(context).saveNews(arrayList);
}
}catch (Exception e) {
e.printStackTrace();
}
return arrayList;
}
//从数据库中获取上次缓存的新闻数据做listview的展示
public static ArrayList<NewsBean> getAllNewsForDatabase(Context context) {
return new NewsDaoUtils(context).getNews();
}
}
2,前文StreamUtils:
public class StreamUtils {
public static String streamToString(InputStream in){
String result ="";
try{
//创建一个字节数组写入流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ( (length = in.read(buffer)) !=-1) {
out.write(buffer, 0, length);
out.flush();
}
result = new String(out.toByteArray(),"gbk");
// result = out.toString();//将字节流转换成string
out.close();
}catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
相关文章推荐
- 解析json数据,明明过程都写的挺对,在网络上请求也有数据,但是就是显示为空,原因是什么???
- OSI七层与TCP/IP五层网络架构,个人理解,通过一个http请求的全过程,分析在协议栈中的流转过程
- Tigase开发笔记6:packet流转机制 -> 一条消息(packet)的请求和响应过程解析
- 【源码解读】Retrofit网络请求过程源码解析
- 原生请求网络,把流转成String
- ping命令全解析(语法、过程、结果分析、网络故障诊断)
- JSONObject 解析从网络获取的json数据时,java.lang.String cannot be converted to JSONObject解决办法(编码 UTF-8)
- 网络请求数据解析
- MKNetwork网络请求过程中onCompletion调用两次的问题
- web请求解析过程
- iOS网络编程(二) 自定义请求网络类----推荐用于需要请求过程片段数据
- IOS学习笔记(16)网络请求,json解析
- 网络请求与解析实例
- 缓存网络请求的结果
- IOS学习笔记,网络请求,json解析
- Unity3D获取网络时间,解析xml/截取string
- IOS中网络数据请求过程详解(GET POST方法使用)
- Darwin Streaming Server中RTSP请求的处理过程(Darwin流媒体服务器解析2)
- 网络中数据传输过程的解析
- 网络请求状态代码含义解析