Gson解析(实战开发二)

闲话少扯,直接代码,一看就明白了:

【MainActivity】

package com.example.testgson;

import java.io.StringReader;

import com.google.gson.stream.JsonReader;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;

public class MainActivity extends Activity {

	private String jsonData="<Data message=\'\' jsondata=\'{\"ds\":[{\"ID\":\"1\",\"Name\":\"智能时代\",\"IPAddress\":\"192.16"
	 +"8.0.0\"}]}\'/>";
	private String sName,sID,sIP;
	private TextView mTitle;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		mTitle = (TextView) findViewById(R.id.title);
		String sJson = jsonData.substring(33);
		Log.d("MainActivity", sJson);
		try {
			JsonReader reader = new JsonReader(new StringReader(sJson));
			reader.beginArray();
			while (reader.hasNext()) {
				reader.beginObject();
				while (reader.hasNext()) {
					String tagName = reader.nextName();
					if (tagName.equals("ID")) {
						sID = reader.nextString();
					} else if (tagName.equals("Name")) {
						sName = reader.nextString();
					} else if (tagName.equals("IPAddress")) {
						sIP = reader.nextString();
					}
				}
				reader.endObject();
			}
			reader.endArray();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Log.d("MainActivity", "ID:"+sID+"--Name:"+sName+"--IPAddress:"+sIP);
		new Thread() {
			@Override
			public void run() {
				Looper.prepare();// 防止报异常
				super.run();
				try {
					handler.sendEmptyMessage(0);
				} catch (Exception e) {
					Log.v("exception", e.getMessage()+ "");
				}
			}
		}.start();
	}
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			int ret = msg.what;
			if (ret == 0) {
				mTitle.setText(sName);
			}
		}
	};
}

【main】XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bj"
    android:orientation="vertical" 
    android:gravity="center_horizontal">
    
<LinearLayout 
	    android:layout_width="fill_parent"
	    android:layout_height="40dp"
	    android:orientation="horizontal">
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:background="@drawable/state_one"
	        android:layout_marginLeft="5dp"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:background="@drawable/state_two"
	        android:layout_marginLeft="10dp"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:background="@drawable/state_three"
	        android:layout_marginLeft="10dp"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:background="@drawable/state_four"
	        android:layout_marginLeft="10dp" />
	</LinearLayout>
	<LinearLayout 
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:gravity="center_horizontal"
	    >
	    <TextView 
	        android:id="@+id/title"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="XXXXXXXX"
	        android:textColor="@android:color/white"
	        android:textSize="35sp"/>
	    </LinearLayout>
	    <LinearLayout 
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:orientation="horizontal"
	        android:layout_marginTop="10dp"
	        android:gravity="center">
	        <LinearLayout 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:orientation="vertical"
	            android:gravity="center"
	            >
	            <ImageView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:background="@drawable/model_btn"/>
	            <TextView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:text="设备模式"
	                android:textSize="20sp"
	                android:textColor="@android:color/white"/>
	        </LinearLayout>
	        <LinearLayout 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:orientation="vertical"
	            android:gravity="center"
	            android:layout_marginLeft="5dp"
	            >
	            <ImageView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:background="@drawable/home_btn"/>
	            <TextView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:text="房屋模式"
	                android:textSize="20sp"
	                android:textColor="@android:color/white"/>
	        </LinearLayout>
	        <LinearLayout 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:orientation="vertical"
	            android:gravity="center"
	             android:layout_marginLeft="5dp"
	            >
	            <ImageView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:background="@drawable/scene_btn"/>
	            <TextView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:text="场景模式"
	                android:textSize="20sp"
	                android:textColor="@android:color/white"/>
	        </LinearLayout>
	        <LinearLayout 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:orientation="vertical"
	            android:gravity="center"
	             android:layout_marginLeft="5dp"
	            >
	            <ImageView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:background="@drawable/high_btn"/>
	            <TextView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:text="搞基模式"
	                android:textSize="20sp"
	                android:textColor="@android:color/white"/>
	        </LinearLayout>
	    </LinearLayout>
</LinearLayout>

Gson解析(实战开发二)_第1张图片

你可能感兴趣的:(android,String,gson)