Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected void onSuccess(T t) {
public void onProgress(int progress, long networkSpeed, boolean done){
}

public void onFailure(int errorCode, String msg) {
public void onFailure(String responseBody, int errorCode, String msg) {
}

public Headers getHeaders() {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected void onSuccess(LoginResponse loginResponse) {

//请求失败(服务返回非法JSON、服务器异常、网络异常)
@Override
public void onFailure(int errorCode, String msg) {
public void onFailure(String responseBody, int errorCode, String msg) {
toast("网络异常~,请检查你的网络是否连接后再试");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.finalteam.okhttpfinal.HttpRequest;
Expand Down Expand Up @@ -57,6 +59,12 @@ protected void onSuccess(JSONObject jsonObject) {
super.onSuccess(jsonObject);
mTvResult.setText(JsonFormatUtils.formatJson(jsonObject.toJSONString()));
}

@Override
public void onFailure(String responseBody, int errorCode, String msg) {
super.onFailure(responseBody, errorCode, msg);
// 返回的类型与期望值不符 本次请求期望后台返回JSONObject,若后台返回JSONArray或其他类型,则会进入此回调方法。
}
});

mMvCode.loadMarkdownFile("file:///android_asset/HttpRequestCallbackJson.md", "file:///android_asset/css-themes/classic.css");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;
import cn.finalteam.okhttpfinal.sample.adapter.NewGameListAdapter;
Expand All @@ -22,6 +23,7 @@
import cn.finalteam.okhttpfinal.sample.widget.swipeview.SwipeRefreshLayout;
import cn.finalteam.okhttpfinal.sample.widget.swipeview.SwipeRefreshLayoutDirection;
import cn.finalteam.toolsfinal.StringUtils;
import okhttp3.Response;
import us.feras.mdv.MarkdownView;

/**
Expand Down Expand Up @@ -111,9 +113,13 @@ public void onLogicFailure(NewGameResponse newGameResponse) {
}

@Override
public void onFailure(int errorCode, String msg) {
super.onFailure(errorCode, msg);
Toast.makeText(getBaseContext(), "网络异常", Toast.LENGTH_SHORT).show();
public void onFailure(String responseBody, int errorCode, String msg) {
super.onFailure(responseBody, errorCode, msg);
if (errorCode == BaseHttpRequestCallback.ERROR_RESPONSE_DATA_PARSE_EXCEPTION) {
Toast.makeText(getBaseContext(), "后台返回类型与期望值不符,客户端需要做容错", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "网络异常", Toast.LENGTH_SHORT).show();
}
}

@Override public void onFinish() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.File;
import java.util.List;

import okhttp3.Response;
import us.feras.mdv.MarkdownView;

/**
Expand Down Expand Up @@ -97,8 +98,8 @@ public void onSuccess(UploadResponse uploadResponse) {
}

@Override
public void onFailure(int errorCode, String msg) {
super.onFailure(errorCode, msg);
public void onFailure(String responseBody, int errorCode, String msg) {
super.onFailure(responseBody, errorCode, msg);
Toast.makeText(getBaseContext(), "上传失败", Toast.LENGTH_SHORT).show();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,15 @@ protected void onPostExecute(ResponseData responseData) {
}

if (callback != null) {
callback.onFailure(code, msg);
callback.onFailure(responseData.getResponse(), code, msg);
}
}
} else {
if (Constants.DEBUG) {
ILogger.d("url=" + url + "\n response failure code=" + code + " msg=" + msg);
}
if (callback != null) {
callback.onFailure(code, msg);
callback.onFailure(responseData.getResponse(), code, msg);
}
}

Expand Down Expand Up @@ -314,7 +314,7 @@ private void parseResponseBody(ResponseData responseData, BaseHttpRequestCallbac
return;
}
}
//接口请求失败
callback.onFailure(BaseHttpRequestCallback.ERROR_RESPONSE_DATA_PARSE_EXCEPTION, "Data parse exception");
//接口请求失败 或 JSON返回类型与期望类型不符导致的解析异常
callback.onFailure(responseData.getResponse(), BaseHttpRequestCallback.ERROR_RESPONSE_DATA_PARSE_EXCEPTION, "Data parse exception");
}
}