admin
2022-05-07 4c7cde7ae5ed57335405459e47de4bbd2726c4ba
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.yeshi.makemoney.video.app.utils.api;
 
import android.content.SharedPreferences;
import android.os.Handler;
 
 
import com.yeshi.makemoney.video.app.MyApplication;
 
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.ParseException;
import org.json.JSONObject;
 
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
 
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.Response;
 
import static android.content.Context.MODE_PRIVATE;
 
public class BasicTextHttpResponsePreHandler implements
        Callback {
 
    public void onStart() {
 
    }
 
    public void onSuccessPerfect(int statusCode, Header[] headers,
                                 JSONObject jsonObject) throws Exception {
    }
 
    public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
 
    }
 
    public void onFailure(int statusCode, Header[] headers, byte[] jsonObject, Throwable e) {
 
    }
 
    public void onFinish() {
    }
 
 
    // 将请求成功的数据返回到主线程进行数据更新
    Handler mainHandler = new Handler(MyApplication.application.getBaseContext().getMainLooper());
 
    @Override
    public void onFailure(Call call, final IOException e) {
        final MyOKHttpHeader header[] = new MyOKHttpHeader[1];
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                onFailure(9999, header, e + "", e);
                onFailure(9999, header, new byte[1], e);
                onFinish();
            }
        });
//        Log.e("mResult", e.toString());
    }
 
    @Override
    public void onResponse(Call call, final Response response) throws IOException {
        final Headers headers1 = response.headers();
        List cookies = headers1.values("Set-Cookie");
        if (cookies.size() > 0) {
            String session = cookies.get(0).toString();
            String sessionid = session.substring(0, session.indexOf(";"));
//            Log.e("mResult", sessionid);
            SharedPreferences sp = MyApplication.application.getBaseContext()
                    .getSharedPreferences("Session", MODE_PRIVATE);
            if (!sp.getString("sessionid", "").equals(sessionid)) {
                SharedPreferences.Editor edit = sp.edit();//编辑文件
                edit.putString("sessionid", sessionid);
                edit.apply();
            }
        }
 
        final String data = response.body().string();
        final MyOKHttpHeader headers[] = new MyOKHttpHeader[headers1.size()];
        int i = 0;
        for (Iterator<String> its = headers1.names().iterator(); its.hasNext(); ) {
            String name = its.next();
            headers[i++] = new MyOKHttpHeader(name, headers1.get(name));
        }
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                onFinish();
                if (response.isSuccessful()) {
                    try {
                        JSONObject jsonObject = new JSONObject(data);
                        onSuccessPerfect(response.code(), headers, jsonObject);
                    } catch (Exception e) {
                        e.printStackTrace();
                        if (data == null || data.length() == 0) {
                            onFailure(response.code(), headers, data, new IOException("Unexpected code " + response));
                            onFailure(response.code(), headers, new byte[1], new IOException("Unexpected code " + response));
                        }
                    }
                } else {
                    onFailure(response.code(), headers, "数据返回失败", new IOException("Unexpected code " + response));
                    onFailure(response.code(), headers, new byte[1], new IOException("Unexpected code " + response));
                }
            }
        });
    }
 
    class MyOKHttpHeader implements Header {
        String name;
 
        String value;
 
 
        public MyOKHttpHeader(String name, String value) {
            this.name = name;
            this.value = value;
        }
 
        @Override
        public String getName() {
            return name;
        }
 
        @Override
        public String getValue() {
            return value;
        }
 
        @Override
        public HeaderElement[] getElements() throws ParseException {
            return new HeaderElement[0];
        }
    }
}