Android简单的获取网络上的json文件
2015-04-07 17:00
561 查看
设置网络访问权限
布局文件:
mian.xml
布局文件:
item.xml
编写主MainActivity文件,具体内容如下:
MainActivity 的核心功能是获取数据并显示数据。
其中获取的数据内的NewsService的具体内容如下:
在getJSONLastNews方法中是直接请求搭建好的web服务器,获取JSON输入流后,使用parseJSON方法解析XML数据并最终返回集合数据。
同时在NewsService中用到了一个JavaBean,用于封装XML数据,其名称News,具体内容如下所示:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wangjialin.internet.json" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="8" /> <!-- 访问internet权限 --> <uses-permission android:name="android.permission.INTERNET"/> </manifest>
布局文件:
mian.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/listView" /> </LinearLayout>
布局文件:
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/title" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/timelength" /> </LinearLayout>
编写主MainActivity文件,具体内容如下:
package com.wangjialin.internet.json; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.wangjialin.internet.json.domain.News; import com.wangjialin.internet.json.service.NewsService; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView listView = (ListView) this.findViewById(R.id.listView); String length = this.getResources().getString(R.string.length); try { List<News> newes = NewsService.getJSONLastNews(); List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>(); for(News news : newes){ HashMap<String, Object> item = new HashMap<String, Object>(); item.put("id", news.getId()); item.put("title", news.getTitle()); item.put("timelength", length+ news.getTimelength()); data.add(item); } SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"title", "timelength"}, new int[]{R.id.title, R.id.timelength}); listView.setAdapter(adapter); } catch (Exception e) { e.printStackTrace(); } } }
MainActivity 的核心功能是获取数据并显示数据。
其中获取的数据内的NewsService的具体内容如下:
package com.wangjialin.internet.json.service; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONObject; import com.wangjialin.internet.json.domain.News; import com.wangjialin.internet.json.utils.StreamTool; public class NewsService { /** * 获取最新视频资讯 * @return * @throws Exception */ public static List<News> getJSONLastNews() throws Exception{ String path = "http://192.168.1.100:8080/ServerForJSON/NewsListServlet"; HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if(conn.getResponseCode() == 200){ InputStream json = conn.getInputStream(); return parseJSON(json); } return null; } private static List<News> parseJSON(InputStream jsonStream) throws Exception{ List<News> list = new ArrayList<News>(); byte[] data = StreamTool.read(jsonStream); String json = new String(data); JSONArray jsonArray = new JSONArray(json); for(int i = 0; i < jsonArray.length() ; i++){ JSONObject jsonObject = jsonArray.getJSONObject(i); int id = jsonObject.getInt("id"); String title = jsonObject.getString("title"); int timelength = jsonObject.getInt("timelength"); list.add(new News(id, title, timelength)); } return list; } }
在getJSONLastNews方法中是直接请求搭建好的web服务器,获取JSON输入流后,使用parseJSON方法解析XML数据并最终返回集合数据。
同时在NewsService中用到了一个JavaBean,用于封装XML数据,其名称News,具体内容如下所示:
package com.wangjialin.internet.json.domain; public class News { private Integer id; private String title; private Integer timelength; public News(){} public News(Integer id, String title, Integer timelength) { this.id = id; this.title = title; this.timelength = timelength; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Integer getTimelength() { return timelength; } public void setTimelength(Integer timelength) { this.timelength = timelength; } }
相关文章推荐
- Android建立网络连接,利用JSON数据获取百度图片搜索结果及GSON的简单使用
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- Android 从网络获取json格式的视频资讯
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- Android 网络通信框架Volley简介以及获取JSON对象和图片加载例子
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- 转:Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- android端获取网络json格式动态生成二维数组
- Android开发18——获取网络资源之json数据
- android 从网络上获取数据(图片,网页,xml,json等)
- android天气查询(二)之网络json数据的获取
- 【android基础】之Android获取网络上的图片结合ImageView的简单应用
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- Android获取网络XML/JSON数据
- android 简单实现获取网络图片
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- Android中数据文件解析(Json解析【从服务器端获取数据并且解析,显示在客户端上面】)
- Android-通过URL获取网络资源 Json 字符串
- Android中通过网络获取json数据来播放视频