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];
|
}
|
}
|
}
|