当前位置 : 主页 > 编程语言 > java >

解析json,gson方法

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt Request.Builder builder = new Request.Builder(); Request build = builder.url("http://192.168.2.5:8080/transportservice/type/jason/action/GetAllSense.do"). post(RequestBody.create(MediaType.parse("json;utf-8"), "")).build(); Ok
gistfile1.txt
Request.Builder builder = new Request.Builder();
        Request build = builder.url("http://192.168.2.5:8080/transportservice/type/jason/action/GetAllSense.do").
                post(RequestBody.create(MediaType.parse("json;utf-8"), "")).build();
        OkHttpClient okHttpClient = new OkHttpClient();

        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                throw new RuntimeException(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Reader reader = response.body().charStream();
                JsonReader jsonReader = new JsonReader(reader);
                jsonReader.beginObject();
                while (jsonReader.hasNext()) {
                    String key = jsonReader.nextName();
                    switch (key) {
                        case "serverinfo":
                            String value = jsonReader.nextString();
                            Gson gson = new Gson();
                            evn_jsons = gson.fromJson(value, Evn_jsons.class);
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    pm2_5.setText(evn_jsons.get_$Pm25275() + "");
                                    wendu.setText(evn_jsons.getTemperature() + "");
                                    shidu.setText(evn_jsons.getHumidity() + "");
OutFragment.java
package mad.com.its02.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.JsonReader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.io.Reader;
import java.util.Timer;
import java.util.TimerTask;

import mad.com.its02.R;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class OutFragment extends Fragment {

    private View view;
    private TextView wendu;
    private TextView shidu;
    private TextView pm2_5;
    private TextView tv;
    private Evn_jsons evn_jsons;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_out, null);
        wendu = (TextView) view.findViewById(R.id.wendu);
        shidu = (TextView) view.findViewById(R.id.shidu);
        pm2_5 = (TextView) view.findViewById(R.id.pm2_5);
        tv = (TextView) view.findViewById(R.id.result);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                getjson();
            }
        }, 1, 2000);


        return view;
    }

    public void getjson() {
        Request.Builder builder = new Request.Builder();
        Request build = builder.url("http://192.168.2.5:8080/transportservice/type/jason/action/GetAllSense.do").
                post(RequestBody.create(MediaType.parse("json;utf-8"), "")).build();
        OkHttpClient okHttpClient = new OkHttpClient();

        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                throw new RuntimeException(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Reader reader = response.body().charStream();
                JsonReader jsonReader = new JsonReader(reader);
                jsonReader.beginObject();
                while (jsonReader.hasNext()) {
                    String key = jsonReader.nextName();
                    switch (key) {
                        case "serverinfo":
                            String value = jsonReader.nextString();
                            Gson gson = new Gson();
                            evn_jsons = gson.fromJson(value, Evn_jsons.class);
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    pm2_5.setText(evn_jsons.get_$Pm25275() + "");
                                    wendu.setText(evn_jsons.getTemperature() + "");
                                    shidu.setText(evn_jsons.getHumidity() + "");

                                    if (evn_jsons.get_$Pm25275() > 100 && evn_jsons.get_$Pm25275() < 0
                                            && evn_jsons.getCo2() < 0 && evn_jsons.getCo2() > 80
                                            && evn_jsons.getTemperature() < 15 && evn_jsons.getTemperature() > 18) {
                                        tv.setText("减少户外活动");
                                    }else {
                                        tv.setText("快出去走走吧");
                                    }
                                }
                            });
                            break;
                        default:
                            break;
                    }
                }
                jsonReader.endObject();
            }
        });
    }
}
Evn_jsons.java
package mad.com.its02.fragment;

import com.google.gson.annotations.SerializedName;

/**
 * Created by Lancer on 2017/11/14.
 */

class Evn_jsons {

    @SerializedName("pm2.5")
    private int _$Pm25275; // FIXME check this code
    private int co2;
    private int LightIntensity;

    @Override
    public String toString() {
        return "Evn_jsons{" +
                "_$Pm25275=" + _$Pm25275 +
                ", co2=" + co2 +
                ", LightIntensity=" + LightIntensity +
                ", humidity=" + humidity +
                ", temperature=" + temperature +
                '}';
    }

    private int humidity;
    private int temperature;

    public int get_$Pm25275() {
        return _$Pm25275;
    }

    public void set_$Pm25275(int _$Pm25275) {
        this._$Pm25275 = _$Pm25275;
    }

    public int getCo2() {
        return co2;
    }

    public void setCo2(int co2) {
        this.co2 = co2;
    }

    public int getLightIntensity() {
        return LightIntensity;
    }

    public void setLightIntensity(int LightIntensity) {
        this.LightIntensity = LightIntensity;
    }

    public int getHumidity() {
        return humidity;
    }

    public void setHumidity(int humidity) {
        this.humidity = humidity;
    }

    public int getTemperature() {
        return temperature;
    }

    public void setTemperature(int temperature) {
        this.temperature = temperature;
    }
}
fragment_out.xml
上一篇:判断单双精度
下一篇:动态代理
网友评论