文章标题
来源:互联网 发布:centos 6.5安装分区 编辑:程序博客网 时间:2024/06/07 19:34
解析请求数据
1:从网上xml形式请求数据。。
public static List getPull(InputStream is) {
List<Zixun> list = null; Zixun bean = null; try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser parser = factory.newPullParser(); parser.setInput(is, "UTF_8"); int type = parser.getEventType(); while (type != XmlPullParser.END_DOCUMENT) { String name = parser.getName(); switch (type) { case XmlPullParser.START_DOCUMENT: list = new ArrayList<Zixun>(); break; case XmlPullParser.START_TAG: if ("news".equals(name)) { bean = new Zixun(); } else if ("title".equals(name)) { bean.title = parser.nextText(); } else if ("body".equals(name)) { bean.body = parser.nextText(); } else if ("commentCount".equals(name)) { bean.commentCount = parser.nextText(); } else if ("author".equals(name)) { bean.author = parser.nextText(); } else if ("pubDate".equals(name)) { bean.pubDate = parser.nextText(); } break; case XmlPullParser.END_TAG: if ("news".equals(name)) { list.add(bean); } break; default: break; } type = parser.next(); } } catch (Exception e) { // TODO: handle exception } //一定要记住这点 return list;}
2给它一个输入流。。。(url,HttpURLConnection )
new Thread(){
public void run() { try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); int code = connection.getResponseCode(); if (code == 200) { //服务器给我们的输入流 InputStream is = connection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] arr = new byte[1024]; int len = 0; while ((len = is.read(arr))!=-1) { baos.write(arr, 0, len); } String result = baos.toString(); //Log.e("TAG", result.toString()); Gson gson = new Gson(); //Type type = new TypeToken<MyData>(){}.getType(); MyData md = gson.fromJson(result, MyData.class); ld = (List<Datas>) md.data; Log.e("TAG111", ld.toString()); hd.sendEmptyMessage(0); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start();public static InputStream getinput(String path){ try { URL url=new URL(path); HttpURLConnection conn=(HttpURLConnection) url.openConnection(); conn.setReadTimeout(5000); conn.setConnectTimeout(5000); if(conn.getResponseCode()==200){ InputStream is=conn.getInputStream(); return is; } } catch (Exception e) { // TODO: handle exception } return null;}
3HttpUtils的请求
String path= “;
HttpUtils hu=new HttpUtils(); hu.send(HttpMethod.GET, path, new RequestCallBack<String>() { @Override public void onFailure(HttpException arg0, String arg1) { } @Override public void onSuccess(ResponseInfo<String> arg0) { //请求成功 String str = arg0.result; Gson gson=new Gson(); News ne= gson.fromJson(str, News.class); List<ListBean> list = ne.data.list; Log.d("TAG", list.toString()); MyAdapter adapter=new MyAdapter(DetailActivity.this,list); lv.setAdapter(adapter); } });}
3Httpclient的请求
public static String getpath(String path){
try {
//创建httpclient HttpClient client=new DefaultHttpClient(); //请求方式 HttpGet get=new HttpGet(path); HttpResponse response = client.execute(get); //得到结果码 int statusCode = response.getStatusLine().getStatusCode(); if(statusCode==200){ return EntityUtils.toString(response.getEntity()); } } catch (Exception e) { // TODO: handle exception } return path;}
1 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- table合并单元格colspan和rowspan
- Factorial Trailing Zeroes 阶乘结果中后缀为0的个数
- Android自助餐之View生命周期
- mapreduce
- 《剑指offer》003-反转链表
- 文章标题
- TabControl 显示和隐藏选项卡
- bzoj 1922(dijkstra)
- 正则表达式
- 捕捉键盘移动(根据键盘移动view跟随上下)
- iOS设计模式
- bzoj1455
- JabRef中文乱码问题解决方法
- 获取所有文件的路径及文件对应标签(文件夹)