Android 解析服务器json数据
2017-02-25 20:33
197 查看
首先看下服务器数据
再看看运行效果
首先是布局:
Activity代码
实体类
权限
再看看运行效果
首先是布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.zking.mygetjson.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/list"></ListView> </LinearLayout>
Activity代码
package com.zking.mygetjson; import android.app.ListActivity; import android.net.Uri; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.TextView; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.zking.entity.BlogsType; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringBufferInputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class MainActivity extends ListActivity { private List<BlogsType> blogsTypes = new ArrayList<>(); private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myAdapter = new MyAdapter(); setListAdapter(myAdapter); new MyTask().execute(); } private class MyTask extends AsyncTask{ @Override protected Object doInBackground(Object[] params) { try { URL url = new URL(getString(R.string.server_name) + "/blogstype/getblogstype"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(5000); InputStream is = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is,"utf-8")); StringBuffer buffer = new StringBuffer(); String s = null; while ((s=bufferedReader.readLine())!=null){ buffer.append(s); } blogsTypes = new Gson().fromJson(buffer.toString(),new TypeToken<List<BlogsType>>(){}.getType()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object o) { super.onPostExecute(o); myAdapter.notifyDataSetChanged(); } } private class MyAdapter extends BaseAdapter{ @Override public int getCount() { return blogsTypes.size(); } @Override public Object getItem(int position) { return blogsTypes.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { LinearLayout linearLayout = new LinearLayout(MainActivity.this); TextView textView = new TextView(MainActivity.this); TextView textView1 = new TextView(MainActivity.this); t 4000 extView.setText(blogsTypes.get(position).getBtid()+""); textView1.setText(blogsTypes.get(position).getBtname()); linearLayout.addView(textView); linearLayout.addView(textView1); return linearLayout; } } }
实体类
package com.zking.entity; /** * Created by Administrator on 2017/2/25. */ public class BlogsType { private int btid; private String btname; private String btintroduce; private String btimage; public int getBtid() { return btid; } public void setBtid(int btid) { this.btid = btid; } public String getBtname() { return btname; } public void setBtname(String btname) { this.btname = btname; } public String getBtintroduce() { return btintroduce; } public void setBtintroduce(String btintroduce) { this.btintroduce = btintroduce; } public String getBtimage() { return btimage; } public void setBtimage(String btimage) { this.btimage = btimage; } }
权限
<uses-permission android:name="android.permission.INTERNET"/>
相关文章推荐
- Android连接服务器数据库查询+Gson解析(List和Map)格式json数据(一)
- android 解析服务器数据使用json还是xml方式
- android客户端+JAVA WEB服务器实现json数据解析
- Android笔记----对服务器XML以及JSON数据的简单解析
- Android连接服务器数据库查询+Gson解析(List和Map)格式json数据(二)
- android中解析服务器发过来的JSON数据
- Android与服务器间数据传递及JSON解析
- Android:真机网络编程、搭建apache服务器、解析JSON数据
- Android解析服务器Json数据实例
- Android 之 网络访问服务器,解析JSON数据(返回无乱码)
- Android使用本地缓存解析远程服务器JSON数据
- android 解析服务器数据使用json还是xml方式
- android解析xml和json数据(获取本地服务器数据)
- 初探Android中的请求服务器并解析返回的json数据
- Android使用GSON和FASTJSON解析服务器返回的JSON数据
- Android获取服务器Json数据与Json的解析
- android开发解析服务器返回的JSON数据
- android 客户端访问自己建立的服务器并返回JSON数据进行解析学习
- Android解析服务器返回的Json数据
- android访问服务器并解析返回的XML和JSON数据