admin
2020-07-14 7af22bf20c862c8ab2270cfeef8f3530f174ac9f
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
package com.yeshi.ec.rebate.myapplication.util;
 
import android.content.SharedPreferences;
import android.os.Handler;
 
import com.yeshi.ec.rebate.myapplication.ShoppingApplication;
 
import org.apache.http.Header;
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(ShoppingApplication.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 = ShoppingApplication.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));
                }
            }
        });
    }
}