import android.os.Handler;
import android.os.Looper;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class Okhehe {
private OkHttpClient client;
private Handler handler;
private MediaType lx= MediaType.parse("application/json;charset=utf-8");
private Okhehe(){
client=new OkHttpClient.Builder().build();
handler=new Handler(Looper.getMainLooper());
}
public static Okhehe getInstance(){
Okhehe hehe=new Okhehe();
return hehe;
}
//Get String
public void GetString(String url, final FuncString callback){
Request request=new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response!=null&&response.isSuccessful()){
ChengGongString(response.body().string(),callback);
}
}
});
}
//Get token String
public void GetTokenString(String url,String token, final FuncString callback){
Request request=new Request.Builder().url(url).addHeader("Authorization",token).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response!=null&&response.isSuccessful()){
ChengGongString(response.body().string(),callback);
}
}
});
}
//Post JSONObejct
public void PostJSONObejct(String url, Map map, final FuncJSONObejct callback){
RequestBody requestBody=RequestBody.create(lx,new Gson().toJson(map));
Request request=new Request.Builder().url(url).post(requestBody).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response!=null&&response.isSuccessful()){
ChengGongJSONObejct(response.body().string(),callback);
}
}
});
}
//put JSONObejct
public void PutJSONObejct(String url,String token, Map map, final FuncJSONObejct callback){
RequestBody requestBody=RequestBody.create(lx,new Gson().toJson(map));
Request request=new Request.Builder().url(url).addHeader("Authorization",token).put(requestBody).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response!=null&&response.isSuccessful()){
ChengGongJSONObejct(response.body().string(),callback);
}
}
});
}
private void ChengGongJSONObejct(final String js, final FuncJSONObejct callback) {
handler.post(new Runnable() {
@Override
public void run() {
JSONObject jo=null;
try {
jo=new JSONObject(js);
} catch (JSONException e) {
e.printStackTrace();
}
callback.onResponse(jo);
}
});
}
private void ChengGongString(final String js, final FuncString callback) {
handler.post(new Runnable() {
@Override
public void run() {
callback.onResponse(js);
}
});
}
public interface FuncString {
void onResponse(String js);
}
public interface FuncJSONObejct {
void onResponse(JSONObject jo);
}
}