lmw
2024-09-04 f4a6d4f0996238f9c85e4986deffe69a1c8256e6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.lotaai.canguiayw;
 
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
 
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
 
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.LogUtils;
import com.lotaai.canguiayw.common.HttpLoggerInterceptor;
import com.lotaai.canguiayw.common.HttpUrlDefine;
import com.lotaai.canguiayw.common.SettingConfig;
import com.pranavpandey.android.dynamic.toasts.DynamicToast;
 
import java.io.IOException;
import java.util.concurrent.TimeUnit;
 
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
 
public class PickCodeListActivity extends AppCompatActivity {
    private LinearLayout llCode;
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_code_list);
        findViewById(R.id.tv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        llCode = findViewById(R.id.ll_code);
        getCode();
    }
 
    public void getCode() {
        HttpLoggerInterceptor loggingInterceptor = new HttpLoggerInterceptor(true);
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(20, TimeUnit.SECONDS)//响应时间,读取时间
                .readTimeout(20, TimeUnit.SECONDS)
                .callTimeout(20, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)//添加日志拦截器
                .build();
 
        RequestBody body = new FormBody.Builder()
                .build();
        final Request request = new Request.Builder()
                .url(HttpUrlDefine.list + "?sn=" + CacheDiskUtils.getInstance().getString(SettingConfig.getInstance().Cache_Device_Code))
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message msg = new Message();
                msg.what = 0;
                msg.obj = "Request failed";
                showHandler.sendMessage(msg);
                LogUtils.e(e);
            }
 
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                LogUtils.i("获取到的数据:" + result);
                final JSONObject obj = JSONObject.parseObject(result);
                if ("200".equals(obj.getString("code"))) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            String[] data = obj.getString("data").split(",");
                            for (int i =0;i<data.length;i++){
                                View view = getLayoutInflater().inflate(R.layout.item_code, llCode, false);
                                ((TextView)view.findViewById(R.id.tv_code)).setText(data[i]);
                                llCode.addView(view);
                            }
                        }
                    });
                } else {
                    Message msg = new Message();
                    msg.what = 0;
                    msg.obj = obj.getString("msg");
                    showHandler.sendMessage(msg);
                }
            }
        });
    }
 
    private Handler showHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                DynamicToast.makeError(PickCodeListActivity.this, msg.obj.toString(), 3).show();
            } else if (msg.what == 1) {
                DynamicToast.makeSuccess(PickCodeListActivity.this, msg.obj.toString(), 3).show();
            } else {
                DynamicToast.makeWarning(PickCodeListActivity.this, msg.obj.toString(), 3).show();
            }
        }
    };
}