Skip to content

Commit

Permalink
OkHttpManeger
Browse files Browse the repository at this point in the history
  • Loading branch information
hubeijiangcan committed Apr 26, 2017
1 parent 580c086 commit f8dd6ca
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 9 deletions.
1 change: 1 addition & 0 deletions Summary/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.squareup.okhttp3:okhttp:3.7.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Created by jc on 2017/4/21.
*/
public class MApplication extends Application {
public class MApplication extends Application {

private static MApplication mInstance;

Expand Down
82 changes: 82 additions & 0 deletions Summary/app/src/main/java/com/mitbbs/summary/OkHttpManeger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.mitbbs.summary;

import android.os.Handler;
import android.os.Looper;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
* Created by jc on 2017/4/26.
*网络请求的api
*/
public class OkHttpManeger {
private OkHttpClient client;
private volatile static OkHttpManeger maneger;
private final String TAG = "OkHttpManeger";
private Handler handler;
//提交json数据
private static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");
//提交字符串
private static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown;charset=utf-8");

private OkHttpManeger(){
client = new OkHttpClient();
handler = new Handler(Looper.getMainLooper());
}

public static OkHttpManeger getInstance(){
OkHttpManeger instance = null;
if (maneger == null){
synchronized (OkHttpManeger.class){
if (instance == null){
instance = new OkHttpManeger();
maneger = instance;
}
}
}
return instance;
}


private void post(final ResponseHandler callBack){
final Request request = new Request.Builder().url(StaticString.BASE_URL).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callBack.onFail(e);
}

@Override
public void onResponse(Call call, Response response) throws IOException {
if (response != null && response.isSuccessful()){
try {
JSONObject object = new JSONObject(response.body().string());
callBack.onSuccess(object);
} catch (JSONException e) {
callBack.onFail(e);
}
}else {
callBack.onFail(new RuntimeException("response is null"));
}
}
});
}
interface ResponseHandler{
void onStart();
void onSuccess(JSONObject object);
void onFail(Exception e);
void onFinish();
}


}
12 changes: 12 additions & 0 deletions Summary/app/src/main/java/com/mitbbs/summary/StaticString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mitbbs.summary;

/**
* Created by jc on 2017/4/26.
* 存放一些常用的静态字符串
*/
public class StaticString {


public static final String BASE_URL = "";

}
8 changes: 0 additions & 8 deletions Summary/app/src/main/java/com/mitbbs/summary/SummaryApi.java

This file was deleted.

0 comments on commit f8dd6ca

Please sign in to comment.