美文网首页技术Android开发程序员
Android笔记 (3): GsonFormat、FastJs

Android笔记 (3): GsonFormat、FastJs

作者: joyin5344 | 来源:发表于2016-04-02 19:46 被阅读787次

上一篇中,已经能够正确拿出api返回结果:

{"code":0,"data":{"country":"\u7f8e\u56fd","country_id":"US","area":"","area_id":"","region":"","region_id":"","city":"","city_id":"","county":"","county_id":"","isp":"","isp_id":"","ip":"21.22.11.33"}}

本节将讲述快速将该结果转换为实体。其中,用到Android Studio的一个插件GsonFormat,该插件可以将json生成对应的java类,而FastJson相信大家都很熟悉了。
GsonFormat安装方法参照该文:
插件GsonFormat快速实现JavaBean

1.Android studio File->Settings..->Plugins–>Browse repositores..搜索GsonFormat

安装GsonFormat插件

2.安装插件,重启android studio

为了从结构上更容易理解,现将MainActivity移至com.joyin.volleydemo.activity包中(记得修改AndroidManifest.xml),然后新建实体类com.joyin.volleydemo.data.api.IpInfo

新建实体类

在IpInfo类中,点击Code->Generate或者右键选Generate都可以使用GsonFormat(快捷键alt+insert)。


使用GsonFormat 输入刚才得到的返回值 点击Form效果更直观 选择需要映射的元素

这里全选即可生成代码

GsonFormat生成的代码

至此,实体类生成已经完成,图多,实际操作步骤实际很少,玩过GsonFormat后真心觉得方便。接下来讲述如何使用fastjson。

FastJson的基本使用

首先,在build.gradle中加入:

compile 'com.alibaba:fastjson:1.2.5'

再次强调,该文章目前只讲述分散的知识技能点,以最原始的方式实现,并非最佳实践方式,后续专题文章中再强调简约、高效、性能等因素。
接下来我们将返回的数据中countrycountry_idip三个字段的值显示出来。首先修改布局文件:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.joyin.volleydemo.MainActivity">

    <TextView
        android:id="@+id/tv_country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_country_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_ip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java

package com.joyin.volleydemo.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.joyin.volleydemo.R;
import com.joyin.volleydemo.data.api.IpInfo;

public class MainActivity extends AppCompatActivity {

    TextView mTvCountry, mTvCountryId, mTvIP;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initData();
    }

    private void initViews() {
        mTvCountry = (TextView) findViewById(R.id.tv_country);
        mTvCountryId = (TextView) findViewById(R.id.tv_country_id);
        mTvIP = (TextView) findViewById(R.id.tv_ip);
    }

    private void initData() {
        String url = "http://ip.taobao.com/service/getIpInfo.php?ip=21.22.11.33";
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.d("demo", "response = " + s);
                IpInfo ipInfo = JSONObject.parseObject(s, IpInfo.class);
                setIpInfoToView(ipInfo);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("demo", "onErrorResponse: " + volleyError.getMessage());
            }
        });

        Volley.newRequestQueue(this).add(request);
    }

    private void setIpInfoToView(IpInfo ipInfo) {
        mTvCountry.setText(ipInfo.getData().getCountry());
        mTvCountryId.setText(ipInfo.getData().getCountry_id());
        mTvIP.setText(ipInfo.getData().getIp());
    }
}

核心代码很简单,没有做空判断等处理,FastJson将json转对象更简单,比如我们的项目中的:

IpInfo ipInfo = JSONObject.parseObject(s, IpInfo.class);

最终运行的效果图:

效果图

相关文章

网友评论

本文标题:Android笔记 (3): GsonFormat、FastJs

本文链接:https://www.haomeiwen.com/subject/jgcylttx.html