您的位置:首页 > 移动开发 > Android开发

Android 解析服务器json数据

2017-02-25 20:33 197 查看
首先看下服务器数据



再看看运行效果



首先是布局:

<?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"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: